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.

import random
for i in range(20):

print '*'*random.choice(range(1,11)

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.

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.

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.

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

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.)

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.

listofnumbers = []
for i in range(0,10):
  listofnumbers.append(input("Give me a number: "))

for i in range(0,10):
  print '*' * listofnumbers[i]

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:

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.

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:

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.

for idx in numberlist:
    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.

print "%02d) %s" % (idx, dd[idx]*'*')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.