我制作了一个python程序,您可以在其中放置3个最喜欢的汽车,并根据需要添加新的汽车。我两天前开始编程,所以很抱歉如果出现一些小问题。任何帮助,例如添加循环,将不胜感激。 谢谢,
print("What are your three favourite cars?")
print("What is your favourite car?")
fav_car = input()
print("What is your 2nd favourite car?")
fav_car2 = input()
print("What is your third favourite car?")
fav_car3 = input()
print("If you want to add another car press a")
a = input()
favourite_cars = [fav_car, fav_car2, fav_car3]
print("Your favourite car is " + favourite_cars[0].title())
print("Your second favourite car is " + favourite_cars[1].title())
print("Your third favourite car is " + favourite_cars[2].title())
if a == "a":
print("What is your 4th car?")
next_car = input()
favourite_cars.append[next_car]
else:
print("Okay, Have a nice day")
favourite_cars = [fav_car, fav_car2, fav_car3, next_car]
print("Your fourth favourite car is " + favourite_cars[3].title())
答案 0 :(得分:2)
您应该使用favorite_cars.append[next_car]
而不是favourite_cars.append(next_car)
,因为append
是一个函数。
另一个较小的更改是,要打印最后一个喜欢的汽车,可以使用favourite_cars[-1].title()
。这只是获取数组最后一个元素的Python技巧。
答案 1 :(得分:1)
您正在使用带有错误括号的.append函数。试试这个:
print("What are your three favourite cars?")
print("What is your favourite car?")
fav_car = input()
print("What is your 2nd favourite car?")
fav_car2 = input()
print("What is your third favourite car?")
fav_car3 = input()
print("If you want to add another car press a")
a = input()
favourite_cars = [fav_car, fav_car2, fav_car3, next_car]
print("Your favourite car is " + favourite_cars[0].title())
print("Your second favourite car is " + favourite_cars[1].title())
print("Your third favourite car is " + favourite_cars[2].title())
if a == "a":
print("What is your 4th car?")
next_car = input()
favourite_cars.append(next_car)
else:
print("Okay, Have a nice day")
favourite_cars = [fav_car, fav_car2, fav_car3, next_car]
print("Your fourth favourite car is " + next_car.title())
答案 2 :(得分:1)
print("What are your three favourite cars?")
fav_car = input("What is your favourite car?")
fav_car2 = input("What is your 2nd favourite car?")
fav_car3 = input("What is your 3rd favourite car?")
a = input("If you want to add another car press a")
favourite_cars = [fav_car, fav_car2, fav_car3]
print("Your favourite car is " + favourite_cars[0].title())
print("Your second favourite car is " + favourite_cars[1].title())
print("Your third favourite car is " + favourite_cars[2].title())
next_car=0
if a == "a":
next_car = str(input("What is your 4th car?"))
else:
print("Okay, Have a nice day")
favourite_cars = [fav_car, fav_car2, fav_car3, next_car]
print("Your fourth favourite car is " + favourite_cars[3].title())
我已经测试了您的代码。我相信问题是您在不需要时添加了favourite_cars。还要清理一下代码(如果要打印问题,只需将问题放入输入中,除非您在问题中使用变量)