为什么python忽略elif和else语句,并且仅基于if语句执行任务,而不管random.choice()的结果如何

时间:2019-01-06 22:22:28

标签: python

它总是印在上面您将是一位了不起的总教练,运气将永远在您身边。如果不正确,它甚至会这样做

usinp = input ("Which scenario would you like to do first 1,2, or 3?")
if usinp == "1":
    print ("You are playing the Packers in OT of the 2014 NFC championship team.")
    firplay = input ("It's your ball on the Seattle 13. The defense is in cover 2. What play do you want to run? HB gut, Hail Mary, or WR screen pass?")
    if firplay == "HB gut":
        import random
        _1 = "Yay you scored a 93 yard touchdown. This scenario is over. YOU WIN"
        _2 = "You Gained 3 yards now it is 2nd and 7 from your own 16"
        _3 = "Your team commited a turnover. This scenario is over. YOU LOSE!"

        PossibleOutcomes = [_1,_2,_3]

        def example():
            print (random.choice(PossibleOutcomes)) 
            if "Yay you scored a 93 yard touchdown. This scenario is over. YOU WIN" in PossibleOutcomes:
                print ("You would be an amazing head coach and luck will always be on your side")

            elif "You Gained 3 yards now it is 2nd and 7 from your own 16" in PossibleOutcomes:
                print("It's your ball on the Seattle 16. The defense is in cover 2. What play do you want to run? Bubble catch, Strong HB toss, Hail Mary?")

            else:
                print("You would be a horrible head coach your team would never make the playoffs and you will be fired.")



        example()

3 个答案:

答案 0 :(得分:2)

您实际上没有存储您的选择。您的测试是检查第一个字符串是否在选项的list中,而不是是否选择了它;由于第一个字符串始终位于选项的list中,因此始终会触发第一个块,而不会测试其他块。对于极简修复:

    def example():
        mychoice = random.choice(PossibleOutcomes)
        print(mychoice) 
        if "Yay you scored a 93 yard touchdown. This scenario is over. YOU WIN" == mychoice:
            print ("You would be an amazing head coach and luck will always be on your side")

        elif "You Gained 3 yards now it is 2nd and 7 from your own 16" == mychoice:
            print("It's your ball on the Seattle 16. The defense is in cover 2. What play do you want to run? Bubble catch, Strong HB toss, Hail Mary?")

        else:
            print("You would be a horrible head coach your team would never make the playoffs and you will be fired.")

答案 1 :(得分:1)

似乎您没有正确保存随机选择的结果,这就是为什么仅执行if语句的原因。您想检查随机选择的结果是否与您的if或elif or else语句匹配。因此,这是此处的逻辑问题。 :)

只需执行此操作即可解决

            choice = random.choice(PossibleOutcomes)
            if "Yay you scored a 93 yard touchdown. This scenario is over. YOU WIN" == choice :
                print ("You would be an amazing head coach and luck will always be on your side")

            elif "You Gained 3 yards now it is 2nd and 7 from your own 16" == choice:
                print("It's your ball on the Seattle 16. The defense is in cover 2. What play do you want to run? Bubble catch, Strong HB toss, Hail Mary?")

            else:
                print("You would be a horrible head coach your team would never make the playoffs and you will be fired.")

哦,欢迎您顺便流一下。 :)还有一个提示:无论外观如何,if和else语句都没有任何问题,这始终只是您逻辑的一部分。从调试的角度来看,打印变量值是检查所发生情况的最简单方法。 :)

答案 2 :(得分:0)

让我们逐行浏览代码:D

# this takes the input for which scenario you want. looking good :)
usinp = input("Which scenario would you like to do first 1,2, or 3?")

# this block of code runs when the user input is 1
if usinp == "1":

    # ooo, cool storyline
    print("You are playing the Packers in OT of the 2014 NFC championship team.")

    # gets another user input
    firplay = input("It's your ball on the Seattle 13. The defense is in cover 2. What play do you want to run? HB gut, Hail Mary, or WR screen pass?")

    # this runs if the input was HB gut
    if firplay == "HB gut":

        # gets the function defined inside random
        import random

        # these are all the choices availiable
        _1 = "Yay you scored a 93 yard touchdown. This scenario is over. YOU WIN"
        _2 = "You Gained 3 yards now it is 2nd and 7 from your own 16"
        _3 = "Your team commited a turnover. This scenario is over. YOU LOSE!"

        # this is the list of possible outcomes. looking good so far
        possibleOutcomes = [_1,_2,_3]

        # a cool little function :D
        def example():

            # this prints out a random choice. hmm, how do you know which choice it is?
            print(random.choice(possibleOutcomes)) 

            # this code checks if that line is in the possible outcomes. it always is :|
            if "Yay you scored a 93 yard touchdown. This scenario is over. YOU WIN" in possibleOutcomes:
                print("You would be an amazing head coach and luck will always be on your side")

            # that flaw on the if statement above means this code will never run...
            elif "You Gained 3 yards now it is 2nd and 7 from your own 16" in possibleOutcomes:
                print("It's your ball on the Seattle 16. The defense is in cover 2. What play do you want to run? Bubble catch, Strong HB toss, Hail Mary?")

            else:
                print("You would be a horrible head coach your team would never make the playoffs and you will be fired.")



        # this runs the flawed function
        example()

您如何解决此问题?只需获取一个变量即可记住是哪个随机选择。

def example():之后的部分替换为:

choice = random.choice(possibleOutcomes)

print(choice)

if "Yay you scored a 93 yard touchdown. This scenario is over. YOU WIN" == choice:
    print("You would be an amazing head coach and luck will always be on your side")
...

TL; DR:将print(random.choice(possibleOutcomes))替换为choice = random.choice(possibleOutcomes) print(choice),然后将in possibleOutcomes替换为== choice。祝你有美好的一天:D