[英]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:问题是当我打印这个时,我得到这个 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.这意味着一旦定义了
random_numbers
和random_colors
,它们就保持不变。 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.
另外,我需要3种以上的组合。 I only have example of two.
我只有两个例子。
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
Think of the random.choice()
as a dice: It produces one value whenever you roll the dice (or call the function).将
random.choice()
视为一个骰子:每当您掷骰子(或调用函数)时,它都会产生一个值。
Think of the variables random_number
and random_color
as the results of the dice roll that you wrote down on a notebook.将变量
random_number
和random_color
视为您在笔记本上记下的掷骰子的结果。 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.) (我在原始问题中使用了单数
random_number
和random_color
而不是复数,因为这些变量中只存储了一个值。)
For repeating the same thing multiple times, use a for
loop.要多次重复同一件事,请使用
for
循环。 (The original while
loop is superfluous as it does not loop because of the break
.) (原来的
while
循环是多余的,因为它没有循环,因为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}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.