为什么这段代码没有按照我想要的方式进行编程?

时间:2019-10-09 16:55:23

标签: python-3.x

当我运行此代码时,程序无法按照我想要的方式进行。 当我回答第二个问题时,程序将执行与我以错误的方式回答第一个问题相同的步骤。
有人可以看到我的错误在哪里吗?

my_money = 100
first_question = input("Who do you bet for? ")

if first_question == "tim" or first_question == "tess" or first_question == "alex" or first_question == "duck" or first_question  == "dog":

    bet = int(input("How much you bet? "))

    if bet < 0:
        print("The bet can't take negative valours! ")
        bet = int(input("How much you bet? "))
        while bet < 0:
            print("The bet can't take negative valours! ")
            bet = int(input("How much you bet? "))

    if bet > my_money:
        print("The bet can't be above the money you have! ")
        bet = int(input("How much you bet? "))
        while bet > my_money:
            print("The bet can't be above the money you have! ")
            bet = int(input("How much you bet? "))

    if bet >= 0 and bet <= my_money:
            total_money = my_money - bet

if first_question != "tim" or first_question != "tess" or first_question != "alex" or first_question != "duck" or first_question != "dog":
    print("There is no runner with that name. ")
    first_question = input("Who do you bet for?")
    while first_question != "tim" or "tess" or "alex" or "duck" or "dog":
        print("There is no runner with that name. ")
        first_question = input("Who do you bet for? ")

1 个答案:

答案 0 :(得分:1)

这是更好的代码版本,我认为对您来说效果不好。

cp1252