如何在用户输入未识别的ans时创建错误消息

2024-09-28 17:28:46 发布

您现在位置:Python中文网/ 问答频道 /正文

def program():

    print ('Start program')

    choice = input("please select either a card or coin?")


    if choice == "COIN":    
        print ("you will now be given heads or tails")
    elif choice == "Coin":
        print ("you will now be given heads or tails")
    elif choice == "coin":
        print ("you will now be given heads or tails")




    import random

    higher_value = 2
    lower_value = 1
    final_value = random.randint (lower_value, higher_value)

    if final_value == 1:
        print ("Heads")

    else:
        print ("Tails")

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                    #CARD

    if choice == "Card":
        print("you will now be given a number for; number,suit")
    elif choice == "card":
        print ("you will now be given a number for; number,suit")
    elif choice == "CARD":
        print ("you will now be given a number for; number,suit")



    import random

    higher_value = 13

    lower_value = 1
    final_value = random.randint (lower_value, higher_value)


    if final_value == 1:
               print ("ace")
    if final_value == 2:
               print ("2")
    if final_value == 3:
               print ("3")
    if final_value == 4:
               print ("4")
    if final_value == 5:
               print ("5")
    if final_value == 6:
               print ("6")
    if final_value == 7:
               print ("7")
    if final_value == 8:
               print ("8")
    if final_value == 9:
               print ("9")
    if final_value == 10:
               print ("10")  
    if final_value == 11:
               print ("Jack")
    if final_value == 12:
               print ("Queen")
    if final_value == 13:
               print ("King")


    import random

    higher_value = 4
    lower_value = 1
    final_value = random.randint (lower_value, higher_value)

    if final_value == 1:
               print ("Hearts")
    if final_value == 2:
               print ("Clubs")
    if final_value == 3:
               print ("Spades")
    if final_value == 4:
               print ("Diamonds")

#------------------------------------------------------------------------------------------------------------------------------
                                    #REPEAT LOOP
flag = True
while flag:
    program()
    flag = input('Would you like to run the program again? [y/n]') == 'y'

print ("The program will now terminate.")

print ("Have a good day")

我正在尝试解决当用户输入错误的数据时如何出错,例如,如果用户输入的是某些内容而不是卡或硬币这两个选项

一个错误信息会出现,然后要求用户重新输入的数据,也当用户是输入卡或硬币,它会产生的结果,卡挑选和投币我想程序输出硬币或卡


Tags: younumberifvaluerandombeprogramlower
2条回答
choice = choice.lower()
if choice == 'card':
    # do the card thing
elif choice == 'coin':
    # do the coin thing
else:
    # print an error message

要回答有关在用户输入错误答案时显示错误的第一个问题,您需要在每次要求用户输入时循环:

choice = input("Please select either a card or coin: ").lower()
while choice not in ('card', 'coin'):
    choice = input("You did not enter a correct choice, please select either a card or coin: ").lower()

如果他们建议用一个大的if语句来写卡片或硬币,那么你需要把你的逻辑包装起来:

if choice == 'card':
    # card logic goes here
elif choice == 'coin':
    # coin logic goes here
else:
    # Shouldn't reach this (your while loop above should catch it)
    # But error anyway, just in case

相关问题 更多 >