0

Why this code is wrong??

I'm wrong in part two and three of this code, but I can't understand. def add(): a = input('Enter your car name: ') car_name.append(a) def remove(car_name): if car_name in car_name: car_name.remove(car_name) else: print('You do not have this car!!!') def show(): for car in car_name: print('Car name = ', car) select = None car_name = [] while select != 4: select = int(input('''1 : show cars list 2 : join cars 3 : remove cars 4 : exit What do you want? ''' )) if select == 1: show() elif select == 2: add() print('added') elif select == 3: car_name = input('enter your car name: ') remove(car_name)

24th Sep 2022, 12:46 PM
Ali Ebrahimi
Ali Ebrahimi - avatar
2 Answers
+ 1
def add(): a = input('Enter your car name: ') car_name.append(a) def remove_car(a): if a in car_name: car_name.remove(a) else: print('You do not have this car!!!') def show(): for car in car_name: print('Car name = ', car) select = None car_name = [] while select != 4: select = int(input('''1 : show cars list 2 : join cars 3 : remove cars 4 : exit What do you want? ''' )) if select == 1: show() elif select == 2: add() print('added') elif select == 3: a = input('enter your car name: ') remove_car(a)
24th Sep 2022, 1:08 PM
Bob_Li
Bob_Li - avatar
+ 1
don't use same variable names.(in remove function) Also renamed remove to remove_car so as not to introduce confusion with list.remove method.
24th Sep 2022, 1:11 PM
Bob_Li
Bob_Li - avatar