1 回答

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
如果您return
從函數(shù)內(nèi)部獲得某些東西,則將其保留-這就是您的 while 循環(huán)不起作用的原因:
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 # EXITS main() - simply delete this row
elif rolledDice == 2 or rolledDice == 3 or rolledDice == 12:
print("YOU LOSE! BETTER LUCK NEXT TIME!")
losses = losses + 1
return losses # EXITS main() - simply delete this row
else:
print("YOU NEITHER WIN NOR LOSE!")
playGame = input("Try again? (Enter yes or no): ")
if playGame == "no":
print("Wins: ", wins)
print("Losses: ", losses)
return # add this to exit the function (could use break as well)
main()
添加回答
舉報(bào)