【问题标题】:Python says my variable is not defined but I defined it correctly in a loopPython说我的变量没有定义,但我在循环中正确定义了它
【发布时间】:2019-01-11 04:00:22
【问题描述】:
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

【问题讨论】:

  • 这里没有循环。如果您指的是elif 中的_2play = input...,那么它只会在这种情况下定义。如果mychoice 是别的什么,在使用的时候不会定义,会抛出异常。顺便说一句 - 每次您想创建 _1_2... 之类的变量时,最好使用 list 之类的数据结构。
  • 我们看不到 firplay 是在哪里定义的,它只在你的代码中出现过一次。
  • 请考虑修改您在此问题中发布的代码示例。就目前而言,它的格式和范围使我们很难为您提供帮助;这是一个great resource,可以帮助您开始。 -1,不要走错路。否决票是我们在这里指出内容问题的方式;改进您的格式和代码示例,我(或有人会)很乐意将其还原。祝你的代码好运!

标签: python python-3.x


【解决方案1】:

根据我理解的代码,有几项。

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

          ... 

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

【讨论】:

    猜你喜欢
    • 2016-08-11
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多