Python说我的变量未定义,但我在一个循环中正确定义了它

时间:2019-01-11 04:00:22

标签: python python-3.x

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

1 个答案:

答案 0 :(得分:0)

据我了解代码,有几项内容。

1)如果直接将_ *字符串放入列表中,可能会更好,例如    在:

PossibleChoices = [“是的,您得分达97码。这种情况已经结束。您赢了,...]

2)那将是mychoice = random.choice(PossibleOutcomes)

3)然后,您可能只需将mychoice与可能的结果[0]进行比较。

4)具有: `         如果“是,您得分97码达阵。这种情况已经结束。您赢了” == mychoice:             打印(“您将成为一名出色的主教练,而运气将永远在您身边”)

    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.")

          ... ```

Of course, it could be further cleaned up.  The above is just something that could solve your problem.