簡體   English   中英

當while循環滿足特定條件時,如何結束程序?

[英]How do I end my program when my while loop has met a certain condition?

我正在上一門計算機科學入門課程,我們現在正在研究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關鍵字將結束最小的封閉循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM