python - 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
解决方案
据我了解代码,有一些项目。
如果您直接将 _* 字符串放入列表中,可能会更好,如下所示:
PossibleChoices = [“是的,你取得了 97 码达阵。这个场景结束了。你赢了”,...]
然后是 mychoice = random.choice(PossibleOutcomes)
然后您可能只需将 mychoice 与 PossibleOutcomes[0] 进行比较。
与:` 如果“是的,你取得了 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.")
...
当然,还可以进一步清理。以上只是可以解决您的问题的东西。
推荐阅读
- angular - 使用 Angular 和 spring boot 设置请求标头
- python - python模块什么时候重新编译?
- powerbi - 如何对相关表中由不同 ID 标识的列进行求和
- javascript - 在下一个 js 中将动态网页转换为静态网页
- c++ - 在模板类中设置动态构造函数参数的方法
- r - 尝试在 R 中捕获 SQL 语句
- typescript - 具有联合类型(字符串和函数)的接口索引签名
- spring - 用于注入静态变量的 Lombok setter
- c# - 如何使用 LINQ 进行分组然后平均
- jwt - 如何使用 Oauth2 使 API 网关无状态以进行身份验证/授权过程?