Python Forum

Full Version: Random number generator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I need help making a random number generator for addition. I have tried to use random.randint but it wont work even with the random import enabled. This generator will need to also see if the users answer is correct. Could somebody please help, I will post the unfinished code bellow.I am also using Thonny, if that changes anything, im not sure. Thanks Smile .

import random


print('Random number generator')
lowest_option=input ('What is the lowest option?')
highest_option=input('What is the highest option?')
for count in range (5):
print random.radint(highest_option,lowest_option)
[quote="charlottelol" pid='130549' dateline='1604919418']
Hello,

I need help making a random number generator for addition. I have tried to use random.randint but it wont work even with the random import enabled. This generator will need to also see if the users answer is correct. Could somebody please help, I will post the unfinished code bellow. Thanks Smile .

import random


print('Random number generator')
lowest_option=input ('What is the lowest option?')
highest_option=input('What is the highest option?')
for count in range (5):
print random.radint(highest_option,lowest_option)
Please use code tags.

input() returns string but random.randint expects integer. This code should raise TypeError while run.
(Nov-09-2020, 11:38 AM)perfringo Wrote: [ -> ]Please use code tags.

input() returns string but random.randint expects integer. This code should raise TypeError while run.

so the line should be

print input(highest_option,lowest_option)

?
I see that there is mix of Python 2 & 3: print('Random number generator') and print random.radint(highest_option,lowest_option). This should be fixed before anything else.

Maybe this can give idea:

>>> import random
>>> start = '2'
>>> end = '5'
>>> random.randint(start, end)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/random.py", line 338, in randint
    return self.randrange(a, b+1)
TypeError: can only concatenate str (not "int") to str
>>> start = int(start)
>>> end = int(end)
>>> random.randint(start, end)
3
Output:
>>> import random >>> help(random.randint) Help on method randint in module random: randint(a, b) method of random.Random instance Return random integer in range [a, b], including both end points. >>> random.randint(1, 5) 2 >>> random.randint('1', '5') Traceback (most recent call last): File "<pyshell#27>", line 1, in <module> random.randint('1', '5') File "C:\Program Files\Python38\lib\random.py", line 248, in randint return self.randrange(a, b+1) TypeError: can only concatenate str (not "int") to str
When you call randint(a, b) the arguments "a" and "b" must be integers. If you try to use a string it will raise the ValueError exception shown above.
Output:
>>> help(input) Help on built-in function input in module builtins: input(prompt=None, /) Read a string from standard input. The trailing newline is stripped. The prompt string, if given, is printed to standard output without a trailing newline before reading input. If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError. On *nix systems, readline is used if available.
lowest_option=input ('What is the lowest option?') returns a string. If you try to use lowest_option as an argument to randint() you will get the same error I got when I tried to use "5". Both are stings, not integers. To get an input range from the user you will use the "input" function, but you have to convert the inputs from a string to an integer. The most common way to do this is use int(str).
Output:
>>> x = input('X: ') X: 5 >>> type(x) <class 'str'> >>> print(x) 5 >>> x = int(x) >>> type(x) <class 'int'> >>> print(x)
There is a lot of confusion about strings and ints and input(). Both times I print(x) the output looks the same even though the first print is printing the string "5" and the second print is printing the int 5.