亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對簡單腳本使用非常低級別的 Python 的微弱嘗試

對簡單腳本使用非常低級別的 Python 的微弱嘗試

波斯汪 2023-03-01 17:05:15
我似乎無法理解為什么這段代碼沒有運行,我是 Python 和整個編程的新手。它返回的錯誤是語法錯誤之一,我們將不勝感激任何幫助。這是我的代碼:heightDescription = ["short", "average", "tall", "very tall"]height = 0if 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])
查看完整描述

4 回答

?
犯罪嫌疑人X

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])


查看完整回答
反對 回復 2023-03-01
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

確保所有條件的末尾if都有一個冒號 ( ):

if abcdef: 
   #

以下是一些非常接近您嘗試的示例。


查看完整回答
反對 回復 2023-03-01
?
MM們

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])


查看完整回答
反對 回復 2023-03-01
?
肥皂起泡泡

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')


查看完整回答
反對 回復 2023-03-01
  • 4 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號