Python-如果满足条件,如何退出程序?

时间:2018-11-19 22:58:59

标签: python

当满足代码开始时的条件时,我无法退出程序,并且当不满足该条件时,它还会阻止其余代码工作。但是,如果从一开始就删除了条件,则代码可以正常工作。一开始如何获得条件才能正常工作?附言:我知道代码可以压缩。

import sys  
user1 = (input("User 1 type your name " )).lower
user2 = (input("User 2 type your name ")).lower
if user1 != "bob":
    sys.exit()
if user2 != "fred":
    sys.exit()

from random import randint
total = 1
score1 = 0
score2 = 0
while total <6:
    roll = (input("User 1 x press 1 to roll the dice "))
    if roll == "x":
        dice = randint(1,6)
        print("You got",dice)
        score1 = score1+dice
        print("User 1 your score is",score1)

    roll2 = (input("\nUser 2 press x to roll the dice "))
    if roll2 == "x":
        dice = randint(1,6)
        print("You got",dice)
        score2 = score2+dice
        print("User 2 your score is",score2)

    total = total+1
    if total == 6:
        print("\nUser1 your total score is",score1)
        print("User2 your total score is",score2)

while total >= 6:
    if score1 == score2:
        print("It's a tie! Whoever rolls the highest number wins")
        roll = (input("User 1 press x to roll the dice"))
        if roll == "x":
            dice = randint(1,6)
            print("You got",dice)
            score1 = score1+dice
            print("User 1 your score is",score1)

        roll2 = (input("\nUser 2 press x to roll the dice"))
        if roll2 == "x":
            dice = randint(1,6)
            print("You got",dice)
            score2 = score2+dice
            print("User 2 your score is",score2)

    if score1 > score2:
        print("\nUser 1 wins")
        break
    if score1 < score2:
        print("\nUser 2 wins")
        break

1 个答案:

答案 0 :(得分:0)

lower实际上是一个方法,您必须调用它。与其写.lower,不如写.lower()