import random
print("Welcome to the Slot Machine Simulator. You'll start with $100 and each spin costs $2. You'll be asked if you want to play. Answer with yes/no. To win you must get one of the following combinations: 3 Cherries: $20, 3 Oranges: $15, 3 Apples: $10, 3 Bananas: $5, 3 Grapes: $4, 3 Bars: $2.50")
Balance = 100
Items = ["Cherries","Oranges", "Apples", "Bananas", "Grapes", "Bars"]
while Balance >= 2:
firstslot = (random.choice(Items))
secondslot = (random.choice(Items))
thirdslot = (random.choice(Items))
Question = input(f"You have {Balance}. Would you like to spin?:")
if Question.casefold() == "yes" or "y":
Balance -= 2
print(firstslot, secondslot, thirdslot)
if (firstslot == "Cherries") and (secondslot == "Cherries") and (thirdslot == "Cherries"):
print("You won $20!")
Balance += 20
elif (firstslot == "Oranges") and (secondslot == "Oranges") and (thirdslot == "Oranges"):
print("You won $15!")
Balance += 15
elif (firstslot == "Apples") and (secondslot == "Apples") and (thirdslot == "Apples"):
print("You won $10!")
Balance += 10
elif (firstslot == "Bananas") and (secondslot == "Bananas") and (thirdslot == "Bananas"):
print("You won $5!")
Balance += 5
elif (firstslot == "Grapes") and (secondslot == "Grapes") and (thirdslot == "Grapes"):
print("You won $4!")
Balance += 4
elif (firstslot == "Bars") and (secondslot == "Bars") and (thirdslot == "Bars"):
print("You won $2.50!")
balance += 2.50
elif Balance < 1.99:
print("Sorry you do not have enough funds to proceed.")
break
else:
print("You lost.")
elif Questions.casefold() == "no" or "n":
break
print("You have ended the game with", Balance, "in your hand.")
else:
print("You have entered an invalid input.")
与用户输入有关的第一个if语句无法正常工作,我不明白为什么。即使有一条if语句表明即使不输入任何内容,它也应该打印出“无效输入”,即使没有输入任何内容,代码也会自动运行。