对简单脚本使用非常低级别的 Python 的微弱尝试

我似乎无法理解为什么这段代码没有运行,我是 Python 和整个编程的新手。它返回的错误是语法错误之一,我们将不胜感激任何帮助。


这是我的代码:


heightDescription = ["short", "average", "tall", "very tall"]


height = 0


if int(height) <= 188:

    print(heightDescription[-1])


if int(height) in range(176, 187)

    print(heightDescription[2])


if int(height) in range(161, 175)

    print(heightDescription[1])


if int(height) in range(1, 174)

    print(heightDescription[0])


波斯汪
浏览 124回答 4
4回答

犯罪嫌疑人X

您在语句后忘记了冒号if:heightDescription = ["short", "average", "tall", "very tall"]height = 0if int(height) <= 188:&nbsp;&nbsp; &nbsp; print(heightDescription[-1])if int(height) in range(176, 187):&nbsp; # <-- Added colon&nbsp; &nbsp; print(heightDescription[2])if int(height) in range(161, 175):&nbsp; # <-- Added colon&nbsp; &nbsp; print(heightDescription[1])if int(height) in range(1, 174):&nbsp; # <-- Added colon&nbsp; &nbsp; print(heightDescription[0])

慕少森

确保所有条件的末尾if都有一个冒号 ( ):if&nbsp;abcdef:&nbsp; &nbsp;&nbsp;&nbsp;#以下是一些非常接近您尝试的示例。

MM们

我还修复了你的程序。你的范围是重叠的,所以有时你会得到两个描述。所以我修复了范围,这里是完整的代码:heightDescription = ["short", "average", "tall", "very tall"]height = 1000if int(height) <= 188:&nbsp; &nbsp; print(heightDescription[0])if int(height) in range(188, 198):&nbsp; &nbsp; print(heightDescription[1])if int(height) in range(198, 208):&nbsp; &nbsp; print(heightDescription[2])if int(height) in range(208, 228):&nbsp; &nbsp; print(heightDescription[3])else:&nbsp; &nbsp; print(heightDescription[3])

肥皂起泡泡

所以你得到的语法错误是由于条件语句中逻辑语句后缺少冒号if。此外,您的逻辑需要工作,因为您最终将获得某些值的多个打印输出。这是您的代码的逻辑更合理的表述:heightDescription = ["short", "average", "tall", "very tall"]height = 0if int(height) >= 187:&nbsp; &nbsp; print(heightDescription[-1])elif int(height) in range(175, 187):&nbsp; &nbsp; print(heightDescription[2])elif int(height) in range(161, 175):&nbsp; &nbsp; print(heightDescription[1])elif int(height) in range(1, 161):&nbsp; &nbsp; print(heightDescription[0])else:&nbsp;&nbsp; &nbsp; print('no height')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python