Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
````
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading