当我的while循环达到某个条件时,如何结束我的程序?

时间:2017-03-23 02:19:12

标签: python while-loop

我刚刚参加了计算机科学的入门课程,我们正在研究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") 

1 个答案:

答案 0 :(得分:4)

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