如何在输入号码时显示有数字的列表


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

我想知道如何得到输出"(他们的选项)是一辆好车"在他们写下他们喜欢的汽车号码后。

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

我们需要打印"their car",所以我们不需要answer==options[index],我们只需要打印选项[0]如果答案是1,选项[1]如果答案是2,选项[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")

检查缩进错误,因为我在写答案时自己编辑了

最新更新