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")
我正在尝试解决当用户输入错误数据时如何出错,例如,如果用户输入某些内容而不是卡或硬币的两个选项。
会出现一条错误消息,然后要求用户重新输入数据,当用户输入卡或硬币时,它也会产生拾卡和抛硬币的结果我希望程序输出硬币或卡片
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