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?

Asked By: trapbuilder2

||

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"

Answered By: Nolan Pestano

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"
Answered By: KaliMachine
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.