943,513 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 626
  • Python RSS
Dec 22nd, 2008
0

list code

Expand Post »
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.
Python Syntax (Toggle Plain Text)
  1. import random
  2. for i in range(20):
  3.  
  4. print '*'*random.choice(range(1,11)
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
tutti is offline Offline
13 posts
since Dec 2008
Dec 22nd, 2008
0

Re: list code

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.

python Syntax (Toggle Plain Text)
  1. def GetGoodAnswer():
  2. answer = raw_input('gimme a number: ')
  3. # Now put in some code to make sure the number is not too big
  4. # or too small, or some string that isn't even a number.
  5. # keep making them enter a number until you get something valid
  6. return ValidNumber
  7.  
  8. # Inside main...
  9. for i in range(0,19):
  10. listofnumbers.append(GetGoodAnswer())

Then you can worry about printing the correct number of asterisks.
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Dec 23rd, 2008
0

Re: list code

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.

Python Syntax (Toggle Plain Text)
  1. gimme a number: 5
  2. Traceback (most recent call last):
  3. File "C:\Users\ADNAN\Documents\imran work\list.py", line 19, in ?
  4. listofnumbers.append(GetGoodAnswer())
  5. File "C:\Users\ADNAN\Documents\imran work\list.py", line 13, in GetGoodAnswer
  6. return ValidNumber
  7. NameError: global name 'ValidNumber' is not defined
Reputation Points: 10
Solved Threads: 1
Newbie Poster
tutti is offline Offline
13 posts
since Dec 2008
Dec 23rd, 2008
0

Re: list code

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.)
Last edited by Murtan; Dec 23rd, 2008 at 12:22 pm. Reason: more comments
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 23rd, 2008
0

Re: list code

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.
python Syntax (Toggle Plain Text)
  1. listofnumbers = []
  2. for i in range(0,10):
  3. listofnumbers.append(input("Give me a number: "))
  4.  
  5. for i in range(0,10):
  6. print '*' * listofnumbers[i]
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Dec 24th, 2008
0

Re: list code

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:
Python Syntax (Toggle Plain Text)
  1. 10) *
  2. 09) * * *
  3. 08)
  4. 07)
  5. 06)
  6. 05) *
  7. 04)
  8. 03)
  9. 02) *
  10. 01)

So the program not only needs to collect the data, but count how many times each number was seen to develop the histogram.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 24th, 2008
0

Re: list code

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:
Python Syntax (Toggle Plain Text)
  1. numlist = [random.choice(range(1,11)) for i in range(n)]
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.
Python Syntax (Toggle Plain Text)
  1. for idx in numberlist:
  2. numberdict[idx] += 1
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.
Python Syntax (Toggle Plain Text)
  1. print "%02d) %s" % (idx, dd[idx]*'*')
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: bit-field conversion in Python
Next Thread in Python Forum Timeline: Improving this program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC