2

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? Also, I need more than 3 combinations. I only have example of two.

2
  • Thats is because you're printing the same random_numbers and random_colors Commented Feb 12, 2021 at 7:42
  • Yeah, can you tell me how to change it? That is my question. I could just make a new variable and do the same thing but I want at least 10 random numbers Commented Feb 12, 2021 at 7:43

2 Answers 2

2
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:
    print(f"These are your cards:\n{random.choice(cards_numbers)} in {random.choice(card_color)}\n{random.choice(cards_numbers)} in {random.choice(card_color)}")
    break

or

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}")

    random_numbers = random.choice(cards_numbers)
    random_colors = random.choice(card_color)
    random_cards = random.choice(other_cards)

    print(f"{random_numbers} in {random_colors}")

    break
Sign up to request clarification or add additional context in comments.

Comments

1

Think of the random.choice() as a dice: It produces one value whenever you roll the dice (or call the function).

Think of the variables random_number and random_color as the results of the dice roll that you wrote down on a notebook. They will not change magically unless you explicitly wipe them out and replace them with a new value.

(I used singular random_number and random_color instead of plural in the original question because only one value stored in these variables.)

For repeating the same thing multiple times, use a for loop. (The original while loop is superfluous as it does not loop because of the break.)

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"]

print(f"These are your cards:")
for _ in range(2):
    random_number = random.choice(cards_numbers)
    random_color = random.choice(card_color)
    print(f"{random_number} in {random_color}")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.