我是编程新手,我的任务是做一个垃圾游戏。游戏一直在进行,直到你被要求再次玩时回答“否”。一旦你输入“否”,它应该显示你赢了和输了多少次。这样做效果很好(不记分):
import random
def main():
playGame = input("Would you like to play Craps? (Enter yes or no): ")
while playGame == 'yes':
roll = input("Press Enter to roll the dice")
rollDice1 = random.randint(1, 6)
rollDice2 = random.randint(1, 6)
print("You got a", rollDice1, "and a", rollDice2)
rolledDice = rollDice1 + rollDice2
print("you rolled a", rolledDice)
if rolledDice == 7 or rolledDice == 11:
print("IT'S YOUR LUCKY DAY! YOU WIN!")
elif rolledDice == 2 or rolledDice == 3 or rolledDice == 12:
print("YOU LOSE! BETTER LUCK NEXT TIME!")
else:
print("YOU NEITHER WIN NOR LOSE!")
playGame = input("Try again? (Enter yes or no): ")
if playGame == "no":
print("Place holder")
main()
当我试图记分时,当你赢或输的时候,它不会循环。(尽管在你不赢也不输的情况下,它仍然会循环):
import random
def main():
wins = 0
losses = 0
playGame = input("Would you like to play Craps? (Enter yes or no): ")
while playGame == 'yes':
roll = input("Press Enter to roll the dice")
rollDice1 = random.randint(1, 6)
rollDice2 = random.randint(1, 6)
print("You got a", rollDice1, "and a", rollDice2)
rolledDice = rollDice1 + rollDice2
print("you rolled a", rolledDice)
if rolledDice == 7 or rolledDice == 11:
print("IT'S YOUR LUCKY DAY! YOU WIN!")
wins = wins + 1
return wins
elif rolledDice == 2 or rolledDice == 3 or rolledDice == 12:
print("YOU LOSE! BETTER LUCK NEXT TIME!")
losses = losses + 1
return losses
else:
print("YOU NEITHER WIN NOR LOSE!")
playGame = input("Try again? (Enter yes or no): ")
if playGame == "no":
print("Wins: ", wins)
print("Losses: ", losses)
main()
我非常感谢您的帮助和建议。就像我说的,我是新手,所以请试着用一种简单的方式来解释什么是错的,我应该做什么
如果从函数内部
return
某个内容,则将其保留—这就是while循环无法工作的原因:不要使用
return
,而是使用yield
。Return结束函数以给出结果,而yield
继续函数相关问题 更多 >
编程相关推荐