【问题标题】:How to make random decisions on variables of variables?如何对变量的变量做出随机决策?
【发布时间】:2023-03-18 04:45:01
【问题描述】:

所以我们有一个学校项目来制作一个程序,询问用户是从 52 副牌中随机选择一张牌还是模拟抛硬币 - 正面还是反面。我有一个测试版,但我正在研究如何让程序更有效率。基本上,该程序将元组 'card' 和 'coin' 设置为所有可能的类型,因此对于硬币、正面和反面以及卡片,有 52 种不同的卡片。

程序的以下部分将变量“选择”设置为卡片或硬币

    while choice!='card' and choice!='coin':
            choice=input("Pick a card or toss a coin (card/coin)?: ")
            if choice!="card" and choice!="coin":
                print("Please enter either 'card' or 'coin'...")
                time.sleep(2)

接下来,我希望能够根据 'choice' 的值从任一元组返回一个值,而无需使用 if 语句,如下所示:

    print("The ",choice," picked at random is... ",random.choice("Either card, or coin"),"!")

我该怎么做?谢谢,布兰登。

完整程序:

#importing modules used in program
import time
import random

#creating duples for possible cards and coins
cards=["Ace of spades","King of spades","Queen of spades","Jack of spades","10 of spades","9 of spades","8 of spades","7 of spades","6 of spades","5 of spades","4 of spades","3 of spades","2 of spades","Ace of diamonds","King of diamonds","Queen of diamonds","Jack of diamonds","10 of diamonds","9 of diamonds","8 of diamonds","7 of diamonds","6 of diamonds","5 of diamonds","4 of diamonds","3 of diamonds","2 of diamonds","Ace of clubs","King of clubs","Queen of clubs","Jack of clubs","10 of clubs","9 of clubs","8 of clubs","7 of clubs","6 of clubs","5 of clubs","4 of clubs","3 of clubs","2 of clubs","Ace of hearts","King of hearts","Queen of hearts","Jack of hearts","10 of hearts","9 of hearts","8 of hearts","7 of hearts","6 of hearts","5 of hearts","4 of hearts","3 of hearts","2 of hearts"]
coin=["Heads","Tails"]

#setting variable 'repeat' to 'y' to start the mainloop of the program
repeat="y"
while repeat=="y":

    #blanking the repeat and choice variables so they can be reset later
    repeat=""
    choice=""

    #won't allow the user to continue until either 'card' or 'coin' is entered
    while choice!='card' and choice!='coin':
        choice=input("Pick a card or toss a coin (card/coin)?: ")
        if choice!="card" and choice!="coin":
            print("Please enter either 'card' or 'coin'...")
            time.sleep(2)


    print("The ",choice," picked at random is... ",random.choice(card),"!")


    #won't allow the user to continue until either 'y' or 'n' is entered
    while repeat!="y" and repeat!="n":
        repeat=input("Go again..? (y/n): ")
        if repeat!="y" and repeat!="n":
            print("Please enter either 'y' for yes or 'n' for no...")
            time.sleep(2)

#ending of the program
print("Goodbye!")
time.sleep(2)

【问题讨论】:

  • 我认为您最好使用简单的 if 检查。

标签: python random


【解决方案1】:

实现这一点的最简单方法是拥有一个元组字典:

items = { 'coin': coin,
          'card': cards }

然后,在您的while 循环正确设置choice 之后,使用用户提供的单词作为键从字典中选择正确的列表,然后可以将其传递给random.choice

print("The ", choice, " picked at random is ...", random.choice(items[choice]))

【讨论】:

  • 只要输入除硬币或卡片以外的任何单词,这将崩溃
  • 我假设这是在给定的while 语句之后使用的,它确保choicecardcoin
  • 是的,我错过了代码中的!=。很难看到夹在一起
【解决方案2】:

我确定这是否是您要问的,但如果您所说的元组是 ('coin', 'card'),那么您可以执行以下操作:

import random

test = ('coin', 'card')
test[random.randint(0, 1)]

这将随机选择两个选项之一。

【讨论】:

  • 不是我想要的
猜你喜欢
  • 1970-01-01
  • 2015-09-30
  • 2014-02-25
  • 2014-02-23
  • 2017-07-11
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
  • 2020-09-21
相关资源
最近更新 更多