+ 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)

2nd Aug 2016, 8:27 PM
Jack
5 Respuestas
+ 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..
3rd Aug 2016, 12:10 AM
Aakash Sharma
Aakash Sharma - avatar
+ 1
import random list=["a", "b"] x= list[random.randint(0, 1)] print(x) i hope it helps.
11th Nov 2016, 4:29 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
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.
2nd Aug 2016, 9:20 PM
Andrev
Andrev - avatar
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.
2nd Aug 2016, 9:21 PM
Andrev
Andrev - avatar
0
hello
2nd Aug 2016, 11:49 PM
desmond