Okay so I'm trying to do this programming assignment and my random function isn't working. It's exactly the same as in my other assignments, but it's not working, and I've spent hours searching it. Can someone help me?

import random

repeat = 0
while repeat != "no":
    random = input("How many numbers do you want?: ")
    w = input(random.randrange(100)+)
    print w*random
    repeat=input("Do you want more random numbers? Yes or No: ")
    if"yes"in repeat:
        random
    
        
raw_input("Press Enter to exit!")

I need to make a program that asks you how many numbers you want (example 3) then it'll print 3 random numbers. And it also needs an exit function (like after you get your x amount of random numbers, you have a choice to quit) Can someone get me started or just show me how to make several different random numbers print at once?

Recommended Answers

All 7 Replies

Well i think your first problem is here:

w = input(random.randrange(100)+)

input is a built in function that only needs a string and gets input from the user. There are a few other things that dont work in your program, like print w*random will just times w by random and give back one number, here is a sample of what i think you mean to do

import random

exit = False
while not exit:
    nums = input("how many numbers?")
    for f in range(nums):
        print random.randrange(0,100),

That will give you the amount of numbers you want, ill leave the exiting for you to figure out.

But do you see how this works?
I get the user input of how many random numbers and then i loop through that many times and print out a random number between zero and one hundred. Then that gets printed out.

Thanks I'll try it in the morning. Mum's bugging me to go to bed because it's 1:00am.

... you should not use random as your variable name. Pick something different, say rand or rnum or whatever. Otherwise you overwrite your imported random module the first time you assign something to variable random. Things will work the first time and fail subsequently.

Here is my way of doing it:

import random

def dieroll(): 
 user_input = int(raw_input("How many random numbers do you want?\n"))
 for each in xrange(user_input):
    randomnumber = random.randrange(100)+1
    print randomnumber

answer = 'y'
while answer == 'y':
    dieroll()
    answer=raw_input("Do you want to repeat this (y=yes, n = no)?")

print " okay bye!"
raw_input("\n\nPress the enter key to exit.")

The user_input allows the user to enter a number.
And when the user enters the number thats when the "for each in xrange(user_input):"
happens. (user_input) is the number the user entered and the number of random numbers it will output.

Ugh my Python GUI isn't working at the moment but here's what I got so far:

import random
z == 0

repeat == 1
while repeat != 2:
    number = input("How many numbers do you want?: ")
    z == random.randrange(100)+1
    for each in xrange(number):
        print z
    repeat=input("Do you want more random numbers? Yes[1] or No[2]: ")

raw_input("Press Enter to exit!")

I've tried yours. It had a error... Why don't you use the one I showed you. I modified it. Instead of entering yes or no, i changed it to 1 or 2. Is that better?

import random

def dieroll(): 
 user_input = int(raw_input("How many numbers do you want?:\n"))
 for each in xrange(user_input):
    randomnumber = random.randrange(100)+1
    print randomnumber

answer = '1'
while answer == '1':
    dieroll()
    answer=raw_input("Do you awnt more random numbers? Yes[1] or No[2]:")


raw_input("\n\nPress enter to exit!")

Yea it's slightly different.

Here is ANOTHER example, you can use this for reference:

# Random number generator

import random
again = 0

raw_input("Press Enter for a random number from 1 to 100!\n")

while (again != 2):
   randomnumber = random.randrange(100)+1
   print randomnumber
   again = input("Do you want to go again? 1 for Yes, 2 for No ")

raw_input("\n\nPress Enter to exit!")

It's ok I got it:

import random
z = 0

repeat = 1
while repeat != 2:
    number = input("How many numbers do you want?: ")
    z = random.randrange(100)+1
    for each in xrange(number):
        print z
        z = random.randrange(100)+1
    repeat=input("Do you want more random numbers? Yes[1] or No[2]: ")

raw_input("Press Enter to exit!")
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.