Reputation: 759
I am using python for producing random in integer between the two value from the user, My code is as follows. I am getting error:
from random import *
i1 = input('Enter the N1 :')
i2 = input('Enter the N2 :')
r = randint({0},{1}.format(i1,i2))
print r
Here I am taking i1 and i2 from user and want to produce random integer between i1 and i2.
I am getting the error:
File "index.py", line 6
r = randint({0},{1}.format(i1,i2))
^
SyntaxError: invalid syntax
Upvotes: 1
Views: 938
Reputation: 20391
To fix everything just do:
r = random.randint(i1, i2)
After fixing the SyntaxError
you will get another error due to passing a string to randint
. Just do what I did above to fix that, you don't need .format
at all.
Upvotes: 4
Reputation: 1509
Here's what you're trying to do:
randint
functionHere's the code to do that:
from random import randint
a = input('Enter the lower bound: ')
b = input('Enter the upper bound: ')
print(randint(a, b))
NOTE: If you're using Python 3 you will need to convert a and b to integers e.g.
a = int(input('Enter the lower bound: '))
Upvotes: 1
Reputation: 11057
According to the manual, input
is equivalent to eval(raw_input(prompt))
, so you don't need to parse strings to integers - i1
and i2
are already integers. Instead do:
from random import randint
i1 = input('Enter N1:')
i2 = input('Enter N2:')
r = randint(i1, i2)
print r
BTW, you are missing quotes for the format string {0},{1}
Upvotes: 1
Reputation: 60024
You need to add quotation marks/apostrophes to make {0},{1}
a string:
r = randint('{0},{1}'.format(i1,i2))
This will pass a string to the random integer function though. The function expects two integers. So all you need to do is:
r = randint(i1, i2)
Upvotes: 2