如何在输入编号时显示包含编号的列表

2024-06-28 19:27:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我想知道如何得到输出“(他们的选择)是一辆好车”后,他们把他们喜欢的汽车数量


Tags: answerinforifexitintoptionsprint
2条回答

我们必须打印“他们的车”,所以我们不需要答案==选项[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")

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

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

相关问题 更多 >