| | |
list code
![]() |
•
•
Join Date: Dec 2008
Posts: 13
Reputation:
Solved Threads: 1
here i have to Write small a program that asks the user to enter 20 numbers (stored in a list), between 1 and 10 and draws a simple of asterisks for the data in the list. For example:
10) * * * * * * *
09) * * *
08) * * * *
07)
06) * *
05) *
04)
03) * * *
02)
01)
and i dont have now clue, how i would start this, if any one can give help to strat, that will be great.
i have made start, but need help fishing it of, see below.
10) * * * * * * *
09) * * *
08) * * * *
07)
06) * *
05) *
04)
03) * * *
02)
01)
and i dont have now clue, how i would start this, if any one can give help to strat, that will be great.
i have made start, but need help fishing it of, see below.
Python Syntax (Toggle Plain Text)
import random for i in range(20): print '*'*random.choice(range(1,11)
•
•
Join Date: Nov 2007
Posts: 141
Reputation:
Solved Threads: 32
I don't understand why you're using the random module at all. Is the user going to supply the numbers or is the program going to generate them? From your description it seems that the user is going to supply the numbers. Start by writing up the code that will ask the user to supply the twenty numbers between one and ten. Also, if you're doing this for homework, remember to throw in some input validation. Professors love that stuff.
Then you can worry about printing the correct number of asterisks.
python Syntax (Toggle Plain Text)
def GetGoodAnswer(): answer = raw_input('gimme a number: ') # Now put in some code to make sure the number is not too big # or too small, or some string that isn't even a number. # keep making them enter a number until you get something valid return ValidNumber # Inside main... for i in range(0,19): listofnumbers.append(GetGoodAnswer())
Then you can worry about printing the correct number of asterisks.
•
•
Join Date: Dec 2008
Posts: 13
Reputation:
Solved Threads: 1
in the code i had to define, the "list of numbers", as there was an error, but now, in the code, it runs , but it still has errors.
below is the code:
def GetGoodAnswer():
answer = raw_input('gimme a number: ')
# Now put in some code to make sure the number is not too big
# or too small, or some string that isn't even a number.
# keep making them enter a number until you get something valid
return ValidNumber
# Inside main...
listofnumbers = []
for i in range(0,19):
listofnumbers.append(GetGoodAnswer())
#print '*'*listofnumbers.(range(0,19)
when i run the code this how it looks, if you can help me out, see wht the problem is, also i need way to print, the asswer, i have print code, but i dont think that will work.
below is the code:
def GetGoodAnswer():
answer = raw_input('gimme a number: ')
# Now put in some code to make sure the number is not too big
# or too small, or some string that isn't even a number.
# keep making them enter a number until you get something valid
return ValidNumber
# Inside main...
listofnumbers = []
for i in range(0,19):
listofnumbers.append(GetGoodAnswer())
#print '*'*listofnumbers.(range(0,19)
when i run the code this how it looks, if you can help me out, see wht the problem is, also i need way to print, the asswer, i have print code, but i dont think that will work.
Python Syntax (Toggle Plain Text)
gimme a number: 5 Traceback (most recent call last): File "C:\Users\ADNAN\Documents\imran work\list.py", line 19, in ? listofnumbers.append(GetGoodAnswer()) File "C:\Users\ADNAN\Documents\imran work\list.py", line 13, in GetGoodAnswer return ValidNumber NameError: global name 'ValidNumber' is not defined
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
His posted code was not intended as a solution, but as a guide into you finding your own solution.
The function "GetGoodAnswer" is full of comments telling you what it should be doing, but it doesn't actually do any of what you see.
And no, that print isn't going to work yet. Once you have your list of 20 numbers from 1 to 10, you have to count how many of each number you got.
Then you can start working on outputting one star for each one you found. (That part might have some of what you had in your print statement.)
The function "GetGoodAnswer" is full of comments telling you what it should be doing, but it doesn't actually do any of what you see.
And no, that print isn't going to work yet. Once you have your list of 20 numbers from 1 to 10, you have to count how many of each number you got.
Then you can start working on outputting one star for each one you found. (That part might have some of what you had in your print statement.)
Last edited by Murtan; Dec 23rd, 2008 at 12:22 pm. Reason: more comments
•
•
Join Date: Nov 2007
Posts: 141
Reputation:
Solved Threads: 32
Well I think I see the problem. I wrote you some pseudo-code to give you a push in the right direction and you tried to use that code verbatim.
In the function you need to (at a minimum) convert the answer that you get from the use (using raw_input) into an integer. Raw_input returns a string. So I'll leave it to you to find out how to change a string into an integer.
Also the function is returning a variable that we haven't initialized. The idea in my pseudocode was that you would take in the string, validate that the string contains only numbers, then convert that to an integer. Make sure the integer is within the bounds that you are expecting. Once you're sure you have a valid answer, then return that valid answer to main.
This is what your basic code should look like.
In the function you need to (at a minimum) convert the answer that you get from the use (using raw_input) into an integer. Raw_input returns a string. So I'll leave it to you to find out how to change a string into an integer.
Also the function is returning a variable that we haven't initialized. The idea in my pseudocode was that you would take in the string, validate that the string contains only numbers, then convert that to an integer. Make sure the integer is within the bounds that you are expecting. Once you're sure you have a valid answer, then return that valid answer to main.
This is what your basic code should look like.
python Syntax (Toggle Plain Text)
listofnumbers = [] for i in range(0,10): listofnumbers.append(input("Give me a number: ")) for i in range(0,10): print '*' * listofnumbers[i]
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
The code at the end of your last post missed the point of the '*' outputs. I'm pretty sure it is supposed to be a histogram of the number of times that a particular value was entered.
So if the input was: 10, 9, 5, 9, 2, 9
The output would be something like:
So the program not only needs to collect the data, but count how many times each number was seen to develop the histogram.
So if the input was: 10, 9, 5, 9, 2, 9
The output would be something like:
Python Syntax (Toggle Plain Text)
10) * 09) * * * 08) 07) 06) 05) * 04) 03) 02) * 01)
So the program not only needs to collect the data, but count how many times each number was seen to develop the histogram.
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
I think Murtan is right about the histogram, but I never would have thought about that from reading the OP's posts. Instead of showing a solution, I will try to describe it and show some sample code.
Get a list of 20 numbers from the user. You can also get a list of 20 random numbers like this: Initialize a dictionary with keys of 1 through 10 with values of 0. This can be done with dict method fromkeys().
Iterate through the number list, and add 1 to the value for the corresponding key in the dictionary. Now all you have to do is format the output. String formatting is ideal for this. Each line of the output could look similar to the following where idx represents the numbers 1 through 10.
Get a list of 20 numbers from the user. You can also get a list of 20 random numbers like this:
Python Syntax (Toggle Plain Text)
numlist = [random.choice(range(1,11)) for i in range(n)]
Iterate through the number list, and add 1 to the value for the corresponding key in the dictionary.
Python Syntax (Toggle Plain Text)
for idx in numberlist: numberdict[idx] += 1
Python Syntax (Toggle Plain Text)
print "%02d) %s" % (idx, dd[idx]*'*')
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- help with adding items to a linked list. (Java)
- Link List Insert problems (C++)
- Completed Code but A LOT of GOTO...Replacement? (C++)
- Using a class to add/delete/show numbers in a Link List (C++)
- find out the error in my code (Java)
- List help (C++)
- Why doesn't this remove the last node in a linked list? (C++)
Other Threads in the Python Forum
- Previous Thread: bit-field conversion in Python
- Next Thread: Improving this program
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming progressbar projects py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython





