首页 > 解决方案 > Python 说我的变量没有定义,但我在循环中正确定义了它

问题描述

if firplay == "HB gut":
        import random
        _1 = "Yay you scored a 97 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!"
        _4 = "You Gained 3 yards now it is 2nd and 7 from your own 16"
        _5 = "You Gained 3 yards now it is 2nd and 7 from your own 16"
        _6 = "You Gained 3 yards now it is 2nd and 7 from your own 16"
        _7 = "You Gained 3 yards now it is 2nd and 7 from your own 16"
        _8 = "You Gained 3 yards now it is 2nd and 7 from your own 16"
        _9 = "You Gained 3 yards now it is 2nd and 7 from your own 16"
        _10 = "You Gained 3 yards now it is 2nd and 7 from your own 16"

        PossibleOutcomes = [_1,_2,_3,_4,_5,_6,_7,_8,_9,_10]
        mychoice = random.choice(PossibleOutcomes)
        print(mychoice)  
        if "Yay you scored a 97 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:
            _2play = input ("It's your ball on the Seattle 16. The defense is in cover 2. What play do you want to run? Bubble catch, Stop and go, or Hook and ladder?")

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

        if _2play == "Bubble catch":
            import random

标签: pythonpython-3.x

解决方案


据我了解代码,有一些项目。

  1. 如果您直接将 _* 字符串放入列表中,可能会更好,如下所示:

    PossibleChoices = [“是的,你取得了 97 码达阵。这个场景结束了。你赢了”,...]

  2. 然后是 mychoice = random.choice(PossibleOutcomes)

  3. 然后您可能只需将 mychoice 与 PossibleOutcomes[0] 进行比较。

  4. 与:` 如果“是的,你取得了 97 码达阵。这个场景结束了。你赢了”== mychoice: print(“你会成为一个了不起的主教练,运气永远在你身边”)

     elif "You Gained 3 yards now it is 2nd and 7 from your own 16" == mychoice:
         _2play = input ("It's your ball on the Seattle 16. The defense is in cover 2. What play do you want to run? Bubble catch, Stop and go, or Hook and ladder?")
    
     else:
         print("You would be a horrible head coach your team will never make the playoffs and you will be fired.")
    
     if _2play == "Bubble catch":
         import random`
    

4a)你不需要再做import random一次。
4b)_2play没有在第二个条件之外的任何地方定义,所以如果用户“获胜”,if _2play == "Bubble catch"则运行,并且由于 _2play 没有在那里定义,它会出错。由于 _2play 与第一个和第三个条件无关,您可以在第二个条件中捕获 _2play 变量。

所以清理后的代码可能是:

if firplay == "HB gut":
    import random

    PossibleOutcomes = [
        "Yay you scored a 97 yard touchdown. This scenario is over. YOU WIN",
        "You Gained 3 yards now it is 2nd and 7 from your own 16",
        "Your team commited a turnover. This scenario is over. YOU LOSE!",
        "You Gained 3 yards now it is 2nd and 7 from your own 16",
        "You Gained 3 yards now it is 2nd and 7 from your own 16",
        "You Gained 3 yards now it is 2nd and 7 from your own 16",
        "You Gained 3 yards now it is 2nd and 7 from your own 16",
        "You Gained 3 yards now it is 2nd and 7 from your own 16",
        "You Gained 3 yards now it is 2nd and 7 from your own 16",
        "You Gained 3 yards now it is 2nd and 7 from your own 16"]

        mychoice = random.choice(PossibleOutcomes)
        print(mychoice)  
        if mychoice == PossibleOutcomes[0]:
            print ("You would be an amazing head coach and luck will always be on your side")
        elif mychoice == PossibleOutcomes[1]:
            _2play = input ("It's your ball on the Seattle 16. The defense is in cover 2. What play do you want to run? Bubble catch, Stop and go, or Hook and ladder?")
            if _2play == "Bubble catch":
                ...
        else:
            print("You would be a horrible head coach your team will never make the playoffs and you will be fired.")

          ... 

当然,还可以进一步清理。以上只是可以解决您的问题的东西。


推荐阅读