python-3.x - Python 3.6 elif 语法错误
问题描述
使用嵌套的 if 语句,我的缩进似乎是正确的,但仍然会出现语法错误。谢谢
# FIGHT Dragons
if ch3 in ['y', 'Y', 'Yes', 'YES', 'yes']:
# WITH SWORD
if sword == 1:
print ("You only have a sword to fight with!")
print ("You quickly jab the Dragon in it's chest and gain an advantage")
time.sleep(2)
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" Fighting... ")
print (" YOU MUST HIT ABOVE A 5 TO KILL THE DRAGON ")
print ("IF THE DRAGON HITS HIGHER THAN YOU, YOU WILL DIE")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(2)
fdmg1 = int(random.randint(3, 10))
edmg1 = int(random.randint(1, 5))
print ("you hit a", fdmg1)
print ("the dragon hits a", edmg1)
time.sleep(2)
if edmg1 > fdmg1:
print ("The drgon has dealt more damage than you!")
complete = 0
return complete
这是我遇到语法错误的地方
elif fdmg1 < 5:
print ("You didn't do enough damage to kill the drgon, but you manage to escape")
complete = 1
return complete
else:
print ("You killed the drgon!")
complete = 1
return complete
解决方案
您的回报必须在 if...elif...else 语句的末尾。这有效:
if edmg1 > fdmg1:
print ("The drgon has dealt more damage than you!")
complete = 0
elif fdmg1 < 5:
print ("You didn't do enough damage to kill the drgon, but you manage to escape")
complete = 1
else:
print ("You killed the drgon!")
complete = 1
return complete
请注意,如果第一个 if 条件为 True,Python 将不会检查后续条件。
推荐阅读
- pandas - 数据集解释房价的连续与分类
- webassembly - 我可以使用 webassembly 来捕获网页的屏幕截图吗?
- java - 如何从 sqlite 数据库创建 pdf 并在 android java 中发送
- python - 需要有关 fetchCurrenciesData 的帮助
- r - R Studio 中更改文件类型的快捷方式
- python - 'list' 对象在构建 one-hot 向量时不可调用
- numpy - Numpy稀疏矩阵按行号选择行
- powershell - 通过 AzureDevopsServer 使用 cls 远程调用 PowerShell 脚本时出现异常设置“ForegroundColor”
- c# - 如何在每个新行上运行 Console.Write()?
- flutter - 如何为一张大图像设置动画以仅在屏幕内显示其自身的一部分?