gpt4 book ai didi

python - 当我的 while 循环满足特定条件时,如何结束我的程序?

转载 作者:太空宇宙 更新时间:2023-11-04 10:02:09 24 4
gpt4 key购买 nike

我刚刚参加了计算机科学入门类(class),我们现在正在研究 Python。我们只需要制作一个简单的游戏来进行一些练习。我的问题是,当播放器达到 $0 时,我希望我的 while 循环结束并退出程序。我的程序只是继续从 0 美元中减去 5 美元。我需要帮助!!!

print ("Welcome to this game!")
password = input ("please put in $50 for 10 tries to play the game,"\
"then type ok.")
while password != ("ok"):
password = input ("Sorry, wrong password. Please put in $50 for 10 tries to play the game,"\
"then type ok.")
if password== ("ok"):
print ("Time to play")

import random
answer=random.randrange(50)+1

total=50

tries=1
guess=int(input("Guess the number and win $100: "))
while guess !=answer:
print ("you loose $5")
if guess < answer:
print ("too low, you now have", total-5, "dollars left.")
elif guess > answer:
print ("too high, you now have", total-5, "dollars left.")
guess=int(input("Guess the number and win $100: "))
tries=tries+1
total=total-5

print ("well done, you have won $100 in", tries,"tries")

最佳答案

break关键字将结束最小的封闭循环。

关于python - 当我的 while 循环满足特定条件时,如何结束我的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42966087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com