我不知道如何修复 AttributeError: 'str' object has no attribute 'append' [duplicate]

问题描述 投票:0回答:1

它给了我同样的错误,我不知道如何修复它:

Traceback (most recent call last):
  File "/tmp/sessions/b41f205e56036d7a/main.py", line 20, in <module>
    car.append
AttributeError: 'str' object has no attribute 'append'

while True:
  car = input("Choose one of the car brands we have recommended.")
  if car.lower() == "q":
    break
  else:
    price = float(input(f"Write a price for the brand of cars from those we have offered you in order to choose which car from these brands to offer you. {car}: $"))
    car.append(car)
    price.append(price)
    
print("----- YOUR CARS -----")

for car in car:
  print(car)
python
1个回答
0
投票

您使用的变量 car 是一个字符串,因此它没有追加方法。您需要创建其他变量来存储汽车和价格。 EG

car_list = []
price_list = []

while True:
  car = input("Choose one of the car brands we have recommended.")
  if car.lower() == "q":
    break
  else:
    price = float(input(f"Write a price for the brand of cars from those we have offered you in order to choose which car from these brands to offer you. {car}: $"))
    car_list.append(car)
    price_list.append(price)
    
print("----- YOUR CARS -----")

for car in car_list:
  print(car)
© www.soinside.com 2019 - 2024. All rights reserved.