简体   繁体   English

如何每次在 Python 中的同一变量上获得不同的随机数?

[英]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_numbersrandom_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_numberrandom_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_numberrandom_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.

相关问题 如何阻止代码重复相同的计算,而是每次都进行随机计算 - How do I stop the code from repeating the same calculation and instead have a random calculation each time Python中如何在循环中每次生成唯一的单个随机数? - How to generate a unique single random number each time in a loop in Python? 如何通过 Python 中的列表中的每个值使列表中的随机值至少在给定数量上是唯一的? - How do I get random values in a list to be unique by at least a given amount by each value in the list in Python? 当人们决定再次玩时,我如何获得不同的随机选择? Python - How do I get a Different Random Choice When People Decided to Play Again? Python 如何从不同的时区获取python daytime.time之间的正确差异? - How do I get the correct difference between python daytime.time from different time zones? 如何在Python中的局部变量中存储随机整数? - How do I store a random integer in a local variable in Python? 如何从python中的集合中打印随机变量 - How do I print a random variable from a set in python 如何创建插入到 python 中的字符串的随机变量? - How do I create a random variable inserted to a string in python? 如何将相同的列表 object 分配给 python 中的两个不同变量名? - How do i assign same list object to two different variable names in python? 每次在 python 中生成一个范围内但大小不同的随机样本 - generate random sample with in a range but with different size each time in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM