jeudi 11 février 2021

How do I get a different random number each time on the same variable in Python?

Here is the code:

import random

cards_numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
card_color = ["red", "yellow", "blue", "green"]
other_cards = ["reverse", "+2", "+4"]

while True:
    random_numbers = random.choice(cards_numbers)
    random_colors = random.choice(card_color)
    random_cards = random.choice(other_cards)
    print(f"These are your cards:\n{random_numbers} in {random_colors}\n{random_numbers} in {random_colors}")
    break

The problem is that when I print this, I get this output:

These are your cards:
5 in yellow
5 in yellow

So that means once random_numbers and random_colors are defined, they stay the same. How can I make it so that with the same variable and in the same print statement, I get a different number and color every time, without making additional variables?




Aucun commentaire:

Enregistrer un commentaire