Creating a random number in a string
Question:
I’m trying to create a variable that is a string that includes a random number, but it keeps telling me that my syntax is invalid, highlighting the r in rand.
import random as rand
Smoke = str(rand.randint(20, 40)" Smoke Shells")
What mistake have I made here?
Answers:
I believe you are getting that syntax error because python is not sure what to do with the provided values.
What I would do is smoke = str(rand.randint(20, 40)) + " Smoke Shells"
There are a few things wrong in the code, like you shouldn’t convert a string to a string
str(" Smoke Shells")
and you can’t add a string to an int.
This should work, though:
import random as rand
Smoke = str(rand.randint(20, 40)) + " Smoke Shells"
I’m trying to create a variable that is a string that includes a random number, but it keeps telling me that my syntax is invalid, highlighting the r in rand.
import random as rand
Smoke = str(rand.randint(20, 40)" Smoke Shells")
What mistake have I made here?
I believe you are getting that syntax error because python is not sure what to do with the provided values.
What I would do is smoke = str(rand.randint(20, 40)) + " Smoke Shells"
There are a few things wrong in the code, like you shouldn’t convert a string to a string
str(" Smoke Shells")
and you can’t add a string to an int.
This should work, though:
import random as rand
Smoke = str(rand.randint(20, 40)) + " Smoke Shells"