+ 1
Why is there a syntax error?
Here is my code, the error is in the syntax (somewhere around random.randint I think) but here it is: import random for i in range(1): letters = random.randint ("a","b") print(letters) (I want it to print either a or b)
5 Antworten
+ 1
I think that the error is in the range "a" & "b" ..
you have entered the strings in a range of random numbers........
in other words ..
a range can have only numbers. Like if one of your friend ask you to tell range between "house" and "horse" you would not be able to answer. But if your friend ask says you to tell a number between range 244 and 678 then you will be able to answer..
in fact rangers are for only numbers...
they are not for strings..
so that is the reason why python is unable to understand your range...
_________________________________________________
--------------------------------------------------------------------------------
in your code you have assigned two strings for ranges..
that are a and b because they are closed in double quotes....... (and python always thinks that the stuff between the double/single quotes is only and only string) ...... and that is why now they are strings ..
please try this code..
import random
a = (you have to also assign it's value)
b= (you have also to assign it's value)
for I in range (5):
letters = random.rundint(a,b)
print(letters)
OK.....
I think that now you are able to understand...
have a nice day..
+ 1
import random
list=["a", "b"]
x= list[random.randint(0, 1)]
print(x)
i hope it helps.
0
randint isn't capable of picking a random element from a list. all randint produces is a random integer in the range specified. so, for instance, randint(0,1) would return either 0 or 1, each with probability 0.5.
0
randint isn't capable of picking a random element from a list. all randint produces is a random integer in the range specified. so, for instance, randint(0,1) would return either 0 or 1, each with probability 0.5.
I'll leave to you the joy of using that to complete your function.
0
hello