简体   繁体   中英

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

I am just taking an introduction course to computer science and we are looking at python right now. We just had to make a simple game for some practice. My problem is that when the player reaches $0 I want my while loop to end and the program to quit. My program just continues subtracting $5 past $0. I need help!!!

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关键字将结束最小的封闭循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM