为什么python忽略elif和else语句,而只基于if语句执行任务,而不管结果如何随机选择()

2024-09-30 00:39:38 发布

您现在位置: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()

Tags: andthetoinyouyourifis
3条回答

似乎您没有正确保存随机选择的结果,这就是为什么只有if语句被执行的原因。您需要检查随机选择的结果是否与if或elif或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.")

哦,顺便说一句,欢迎来这里。:)还有一个额外的提示:不管它看起来如何,如果和else语句没有任何问题,它总是你的逻辑的一小部分。打印变量值是从调试的角度检查正在发生的事情的最简单的方法。:)

让我们逐行检查代码: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:用choice = random.choice(possibleOutcomes)print(choice)替换print(random.choice(possibleOutcomes)),然后用== choice替换in possibleOutcomes。祝你今天愉快:D

你实际上没有储存你的选择。您的测试是检查第一个字符串是否在选项的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.")

相关问题 更多 >

    热门问题