0

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))
2
  • Well what is the result you wanted? Commented Mar 15, 2018 at 15:19
  • 1
    You already have answers, but let me give some different advice: You're making a mistake many beginning programmers make by using strings for everything. Strings are slow and complicated; computers like numbers. Try to train yourself to avoid strings unless they are really necessary. Commented Mar 15, 2018 at 17:40

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.