4 回答
TA贡献2080条经验 获得超4个赞
您在语句后忘记了冒号if:
heightDescription = ["short", "average", "tall", "very tall"]
height = 0
if int(height) <= 188:
print(heightDescription[-1])
if int(height) in range(176, 187): # <-- Added colon
print(heightDescription[2])
if int(height) in range(161, 175): # <-- Added colon
print(heightDescription[1])
if int(height) in range(1, 174): # <-- Added colon
print(heightDescription[0])
TA贡献1886条经验 获得超2个赞
我还修复了你的程序。你的范围是重叠的,所以有时你会得到两个描述。
所以我修复了范围,这里是完整的代码:
heightDescription = ["short", "average", "tall", "very tall"]
height = 1000
if int(height) <= 188:
print(heightDescription[0])
if int(height) in range(188, 198):
print(heightDescription[1])
if int(height) in range(198, 208):
print(heightDescription[2])
if int(height) in range(208, 228):
print(heightDescription[3])
else:
print(heightDescription[3])
TA贡献1829条经验 获得超6个赞
所以你得到的语法错误是由于条件语句中逻辑语句后缺少冒号if。
此外,您的逻辑需要工作,因为您最终将获得某些值的多个打印输出。
这是您的代码的逻辑更合理的表述:
heightDescription = ["short", "average", "tall", "very tall"]
height = 0
if int(height) >= 187:
print(heightDescription[-1])
elif int(height) in range(175, 187):
print(heightDescription[2])
elif int(height) in range(161, 175):
print(heightDescription[1])
elif int(height) in range(1, 161):
print(heightDescription[0])
else:
print('no height')
添加回答
举报