Home > Enterprise >  why is the input after the def not functioning the second time around after the first if statement
why is the input after the def not functioning the second time around after the first if statement

Time:12-31

import sys

make_list = []
wheel_base_list = []
horse_power_list = []


def main():
    sys.stdout.write("Menu\n====\n"
                     "[1] Add a record\n"
                     "[2] Display all records\n"
                     "[3] Exit\n"
                     "Choice: ")
       

main()

option = int(sys.stdin.readline())

    
while option != 0:

    if option == 1:
        sys.stdout.write ("Enter Car make: \n")
        make = sys.stdin.readline().strip()
        make_list.append(make)
        sys.stdout.write("Enter Car Wheel Base: \n")
        car_wheel = str(sys.stdin.readline())
        wheel_base_list.append(car_wheel)
        sys.stdout.write("Enter Car Horse Power: \n")
        horse_power = str(sys.stdin.readline())
        horse_power_list.append(horse_power)
        sys.stdout.write ("Enter Car make: \n")
        
        break
        main()   
        
    elif option == 2:
        sys.stdout.write(make_list[1])
       
        main()
    
    elif option == 3:    
        sys.stdout.write("Cheerio")
        break

    else:
            sys.stdout.write("invalid option")

            
main()

i want to be able to go back to the menu and be able to choose between adding more to the lists or displaying the records or ending th program, but after the menu def displays a second time the int input won't work? or the int i type just wont be accepted as an input for the selection of something on the menu i have created. i have no idea why

CodePudding user response:

The problem comes from these lines:

break
main()

Once you break out of the while loop, the main() call doesn't get run. There are other problems with the code. For instance if the user chooses 4, the progam keeps printing 'invalid option' indefinitely. Also, why don't you use the print and input functions? Here is the code using these functions:

make_list = []
wheel_base_list = []
horse_power_list = []

while True:
    print("Menu\n====\n[1] Add a record\n[2] Display all records\n[3] Exit\nChoice: ")
    
    option = int(input())
    
    if option == 1:
        print("Enter Car make: ")
        make = input()
        make_list.append(make)
        print("Enter Car Wheel Base: ")
        car_wheel = input()
        wheel_base_list.append(car_wheel)
        print("Enter Car Horse Power: ")
        horse_power = input()
        horse_power_list.append(horse_power)
        print("Enter Car make: ")
        

    elif option == 2:
        print(make_list)

    elif option == 3:    
        print("Cheerio")
        break

    else:
        print("invalid option")

I hope this helps!

CodePudding user response:

There are some logic errors in your code, but the bug at hand can be solved using input() function which is a pre-defined function in python for which you don't need to import anything.

import sys

make_list = []
wheel_base_list = []
horse_power_list = []


def main():
    sys.stdout.write("Menu\n====\n"
                     "[1] Add a record\n"
                     "[2] Display all records\n"
                     "[3] Exit\n"
                     "Choice: ")
       

main()

option = int(input())

    
while option != 0:

    if option == 1:
        sys.stdout.write ("Enter Car make: \n")
        make = input().strip()
        make_list.append(make)
        sys.stdout.write("Enter Car Wheel Base: \n")
        car_wheel = str(input())
        wheel_base_list.append(car_wheel)
        sys.stdout.write("Enter Car Horse Power: \n")
        horse_power = str(input())
        horse_power_list.append(horse_power)
        #sys.stdout.write ("Enter Car make: \n")
        
        break
        main()   
        
    elif option == 2:
        sys.stdout.write(make_list[1])
       
        main()
    
    elif option == 3:    
        sys.stdout.write("Cheerio")
        break

    else:
            sys.stdout.write("invalid option")

            
main()

as for running the entire code and going back to your menu, you need to either come up with your own logic or perhaps surf the google a little and you'll easily find some code for it!

  • Related