Jul-10-2020, 02:21 PM
Hi, I am trying to program a Car Application menu loop to display the list of cars and add on cars according to the userinput 1 and 2 respectively.
In the end ,after the user adds on as many cars as they want and decides to display the list of cars once again by inputting 1, the list displayed should include the additional cars they added.
In the end ,after the user adds on as many cars as they want and decides to display the list of cars once again by inputting 1, the list displayed should include the additional cars they added.
#Display Main Menu print('*'*100+'\n'+' '*44+'PRACTICAL\n'+' '*44+'MAIN MENU\n'+ '1. Display List of Cars\n' + '2. Add Car\n' + '\nEnter q to quit program\n'+'*'*100) #Set an initial value for choice. userinput = '' #Start a loop that runs until the user enters the value 'quit' to exit the program. while userinput != 'q': userinput = input("\nWhat would you like to do? ") #If user chooses 1 to Display Car List if userinput == '1': print("\nYou have decided to display the list of cars.") print("\n") #Import pandas as pd import pandas as pd carList = [['Toyota Prius', 'MPV', 'Hybrid', '140000'], ['Honda City', 'Saloon', 'Economical', '75000'], ['Subaru Forester', 'SUV', 'Adventure', '140000'], ['Honda Jazz', 'Hatchback', 'Compact', '70000'], ['Tesla Model 3', 'Coupe', 'Electric Vehicle', '120000'], ['Mercedes-Benz', 'Cabriolet', 'Turbo-charged', '250000']] df = pd.DataFrame(carList, columns =['Model', 'Type', 'Spec', 'Price']) print(df) print('*'*100) #If user chooses 2 to Add Car elif userinput == '2': print("\nYou have decided to add a car.") newmodel=input("What is the model of the new car? ") newtype=input("What is the type of the new car? ") newspec=input("What is the spec of the new car? ") newprice=input("What is the price of the new car? ") print("\n") to_append = [str(newmodel),str(newtype),str(newspec),str(newprice)] df_length = len(df) df.loc[df_length] = to_append print(df) print('*'*100) #If user chooses q to Quit Program elif userinput == 'q': print("\nThis is the end of the Car Application.\n")However, the list is not updated at the end as shown. I am unsure as to why is this so.
Output:****************************************************************************************************
PRACTICAL
MAIN MENU
1. Display List of Cars
2. Add Car
Enter q to quit program
****************************************************************************************************
What would you like to do? 1
You have decided to display the list of cars.
Model Type Spec Price
0 Toyota Prius MPV Hybrid 140000
1 Honda City Saloon Economical 75000
2 Subaru Forester SUV Adventure 140000
3 Honda Jazz Hatchback Compact 70000
4 Tesla Model 3 Coupe Electric Vehicle 120000
5 Mercedes-Benz Cabriolet Turbo-charged 250000
****************************************************************************************************
What would you like to do? 2
You have decided to add a car.
What is the model of the new car? BMW
What is the type of the new car? SUV
What is the spec of the new car? Economical
What is the price of the new car? 135000
Model Type Spec Price
0 Toyota Prius MPV Hybrid 140000
1 Honda City Saloon Economical 75000
2 Subaru Forester SUV Adventure 140000
3 Honda Jazz Hatchback Compact 70000
4 Tesla Model 3 Coupe Electric Vehicle 120000
5 Mercedes-Benz Cabriolet Turbo-charged 250000
6 BMW SUV Economical 135000
****************************************************************************************************
What would you like to do? 2
You have decided to add a car.
What is the model of the new car? Honda
What is the type of the new car? Hatchback
What is the spec of the new car? Compact
What is the price of the new car? 102000
Model Type Spec Price
0 Toyota Prius MPV Hybrid 140000
1 Honda City Saloon Economical 75000
2 Subaru Forester SUV Adventure 140000
3 Honda Jazz Hatchback Compact 70000
4 Tesla Model 3 Coupe Electric Vehicle 120000
5 Mercedes-Benz Cabriolet Turbo-charged 250000
6 BMW SUV Economical 135000
7 Honda Hatchback Compact 102000
****************************************************************************************************
What would you like to do? 1
You have decided to display the list of cars.
Model Type Spec Price
0 Toyota Prius MPV Hybrid 140000
1 Honda City Saloon Economical 75000
2 Subaru Forester SUV Adventure 140000
3 Honda Jazz Hatchback Compact 70000
4 Tesla Model 3 Coupe Electric Vehicle 120000
5 Mercedes-Benz Cabriolet Turbo-charged 250000
****************************************************************************************************
I would really appreciate any help. Thank you for your time!!