list code

Reply

Join Date: Dec 2008
Posts: 13
Reputation: tutti is an unknown quantity at this point 
Solved Threads: 1
tutti tutti is offline Offline
Newbie Poster

list code

 
0
  #1
Dec 22nd, 2008
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.
  1. import random
  2. for i in range(20):
  3.  
  4. print '*'*random.choice(range(1,11)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 141
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster

Re: list code

 
0
  #2
Dec 22nd, 2008
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: tutti is an unknown quantity at this point 
Solved Threads: 1
tutti tutti is offline Offline
Newbie Poster

Re: list code

 
0
  #3
Dec 23rd, 2008
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.

  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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: list code

 
0
  #4
Dec 23rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 141
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster

Re: list code

 
0
  #5
Dec 23rd, 2008
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.
  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]
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: list code

 
0
  #6
Dec 24th, 2008
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: list code

 
0
  #7
Dec 24th, 2008
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:
  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.
  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.
  1. print "%02d) %s" % (idx, dd[idx]*'*')
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC