Seth Puhala
Seth Puhala

Reputation: 45

Creating variable out of random output

So I'm creating a basic (my first project using python) game with python. there is a part where I put a random.choice. I want to refer back to the same random number that it picked so I wondered if it is possible to create a variable for that output. I've tried str = randomint(1,7) but that didnt give me the result I wanted.

# random module
import random
dice1 = ['1','2','3','4','5','6','7']
print (random.choice(dice1))

Upvotes: 0

Views: 44

Answers (2)

Grayson Briggs
Grayson Briggs

Reputation: 69

Here is how you would generate and then store a random number in Python. If you want a number between two numbers use random.randint(a,b). Note that using randint will give you an int and not a string

import random
number = random.randint(1,7)
print(number)

Upvotes: 2

dfundako
dfundako

Reputation: 8314

Your use of random.choice is indeed giving you a random selection from your list dice1. You can store the return value of random.choice in a variable.

# random module
import random
dice1 = ['1','2','3','4','5','6','7']
random_number = random.choice(dice1)

Upvotes: 0

Related Questions