Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random number generator
#1
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)
Reply
#2
[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)
Reply
#3
Please use code tags.

input() returns string but random.randint expects integer. This code should raise TypeError while run.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(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)

?
Reply
#5
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,198 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  matrix number assignement to the random indices juniorcoder 4 1,915 Feb-19-2022, 02:18 PM
Last Post: juniorcoder
  Random coordinate generator speed improvement saidc 0 2,048 Aug-01-2021, 11:09 PM
Last Post: saidc
  Generate random hex number ZYSIA 1 11,542 Jul-16-2021, 09:21 AM
Last Post: DeaD_EyE
  Random Number Repeating Tzenesh 5 4,021 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  Sequence Generator - Long number krux_rap 3 2,311 Aug-19-2020, 06:37 PM
Last Post: buran
  basic random number generator in replace function krug123 2 2,033 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Help with a random number generator dpcalder 2 2,324 Jun-20-2020, 03:50 AM
Last Post: buran
  Generate only one random number for many tries Bhavika 2 1,731 Mar-29-2020, 12:12 PM
Last Post: Bhavika
  Can i prevent the random generator to generate already used numbers? MauserMan 3 2,860 Jan-05-2020, 04:44 PM
Last Post: MauserMan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020