I am trying to write a piece of code as an assignment that terminates by a predefined input

The code I am trying to write would continuously loop and terminates after a predefined input from the user. It would also do some calculations and print at the end before starting the over. The code that I have written so far below loops, but it does not terminate with the predefined user input. Please help – DC.20212833

salesp_num = ' '
while salesp_num != 00000:

salesp_num  = input("Please enter the salesperson's number or enter 00000 to exit:\n")

if len(salesp_num) != 5:
    print("The salesperson's number must be 5 digits\n")

else:
    salesp_num = int(salesp_num)

    salesamount = float(input("Enter the sales amount:\n"))
    Class = int(input("What is the class of the sales person? 1, 2, or 3?\n"))

    if Class == 1:
        if salesamount <= 1000:
            commission = salesamount * 0.06
        elif salesamount > 1000 and salesamount < 2000:
            commission = salesamount * 0.07
        elif salesamount > 2000:
            commission = salesamount * 0.1
    elif Class ==2:
        if salesamount < 1000:
            commission = salesamount * 0.04
        elif salesamount >= 1000:
            commission = salesamount * 0.06
    elif Class ==3:
        commission = salesamount * 0.045
    else:
        print("Incorrect class")


    print(salesp_num)
    print(Class)
    print(salesamount)
    print(commission)

>Solution :

You can change it as below. Note, I have used ‘0’ for exit, not ‘00000’

salesp_num = ' '

while (salesp_num != '0'):
    salesp_num  = input("Please enter the salesperson's number or press '0' to exit:\n")
    if salesp_num == '0':
        print("Exiting....")
        break
        
    if len(salesp_num) != 5:
        print("The salesperson's number must be 5 digits\n")

    else:
        salesp_num = int(salesp_num)

        salesamount = float(input("Enter the sales amount:\n"))
        Class = int(input("What is the class of the sales person? 1, 2, or 3?\n"))

        if Class == 1:
            if salesamount <= 1000:
                commission = salesamount * 0.06
            elif salesamount > 1000 and salesamount < 2000:
                commission = salesamount * 0.07
            elif salesamount > 2000:
                commission = salesamount * 0.1
        elif Class ==2:
            if salesamount < 1000:
                commission = salesamount * 0.04
            elif salesamount >= 1000:
                commission = salesamount * 0.06
        elif Class ==3:
            commission = salesamount * 0.045
        else:
            print("Incorrect class")


        print(salesp_num)
        print(Class)
        print(salesamount)
        print(commission)
````

Leave a Reply