Python说我的变量没有定义,但我在循环中正确地定义了它

2024-04-18 22:37:08 发布

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

Tags: andfromyouyourifisitrandom
2条回答

据我所知,代码中有几项。你知道吗

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

PossibleChoices=[“耶,你触地得分97码。这种情况已经结束了。你赢了“,…]

2)那就是我的选择=随机选择(可能的时间)

3)然后您可能会将mychoice与PossibleOutcomes[0]进行比较。你知道吗

4)带: ` 如果你在97码处触地得分。这种情况已经结束了。你赢了“==我的选择: 打印(“你将是一个了不起的主教练,幸运永远在你身边”)

    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在第2个条件中没有定义,因此如果用户“赢了”,则运行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.


相关问题 更多 >