python - if 语句 & else 没有按预期工作
问题描述
if b1a == "rock":
print("you gave him a concussion and made the rock your new weapon")
weapon = "rock"
else:
print("you had a heart attack because you didn't pick one of the options")
exit()
if b1a == "NIGERUNDAYO":
weapon = "tommy gun"
print("You ran so much that the beast got tired, then you took out a tommy gun and started shooting")
else:
print("you had a heart attack because you didn't pick one of the options")
exit()
if b1a == "slash":
print("you slashed his face with a hatchet. ")
else:
print("you had a heart attack because you didn't pick one of the options")
exit()
它总是让我心脏病发作。
我是初学者,想知道是否有其他方法可以解决这个问题。
解决方案
最好elif
每次只使用而不是if-else
构造。
if b1a == "rock":
print("you gave him a concussion and made the rock your new weapon")
weapon = "rock"
elif b1a == "NIGERUNDAYO":
weapon = "tommy gun"
print("You ran so much that the beast got tired, then you took out a tommy gun and started shooting")
elif b1a == "slash":
print("you slashed his face with a hatchet. ")
else:
print("you had a heart attack because you didn't pick one of the options")
exit()
推荐阅读
- python - 如何将一个表中的 id 插入到另一个表中?
- xml - xslt/XML 消除重复的属性值
- jquery - 自定义滚动事件
- swift - 以编程方式快速呈现一个新的视图控制器/以编程方式给一个类一个标识符
- ruby - 如果键有多个匹配项,如何从哈希中获取特定数据?
- accessibility - 如何强制屏幕阅读器发音?
- selenium - contextClick() 不适用于 IEWebDriver
- python - 在python中打印json数据应该反映在kibana中的json文件
- stackdriver - Stackdriver Monitoring:自定义指标问题
- php - 如何在codeignitor中显示来自MYSQL的数据?