options = ["1. Hyundai", "2. Benz", "3. BMW", "4. Honda", "5. Toyota", "6. GMC", "7. Cadillac", "0.Exit"]
for i in options:
print(i)
answer = int(input("choose a number from the list above "))
for index in range(len(options)):
if answer == options[0]:
print(f"{options} is a nice car")
if answer == 0:
print("Goodbye")
我想知道如何在他們輸入他們喜歡的汽車號碼后獲得“(他們的選擇)是一輛不錯的汽車”的輸出。
uj5u.com熱心網友回復:
options = ["1. Hyundai", "2. Benz", "3. BMW", "4. Honda", "5. Toyota", "6. GMC", "7. Cadillac", "0.Exit"]
for i in options:
print(i)
answer = int(input("choose a number from the list above "))
if answer == 0:
print("Goodbye")
else:
print(f"{options[answer-1]} is a nice car")
uj5u.com熱心網友回復:
我們必須列印“他們的車”,所以我們不需要 answer==options[index],我們只需要列印 options[0] 如果答案是 1,options[1] 如果答案是 2,options[2]如果答案是 3,依此類推,那么您編輯的代碼將是:
options = ["1. Hyundai", "2. Benz", "3. BMW", "4. Honda", "5. Toyota",
"6. GMC", "7. Cadillac", "0.Exit"]
for i in options:
print(i)
answer = int(input("choose a number from the list above "))
if answer in range(1,8):
print(f"{options[answer-1]} is a nice car")
elif answer == 0:
print("Goodbye")
else:
print("wrong option chosen")
檢查縮進錯誤,因為我在寫答案時自己編輯過
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/312272.html
