[Tutor] Python program malfunction

Jag Sherrington braveart08 at yahoo.com.au
Mon May 4 06:26:11 CEST 2015


Hi,
There appears to be a problem with this program.
It is taken from the "Starting out with Python" book third edition.

The problem is when the "validate the wholesale cost" is introduced.
Without the validator the code works fine, but with it the code won't let you enter a positive wholesale cost unless you do a negative cost first. 
Even after you have entered a negative cost and got the results of your positive cost it asks wether you want to do another item if you type "y" you still can't enter a positive amount.

HERE IS THE CODE AS COPIED FROM THE BOOK:

#This program calculates retail prices.

mark_up = 2.5  # The mark up percentage.
another = 'y'   # Variable to control the loop.

# Process one or more items.
while another == 'y' or another == 'y':
    #Get the item 's wholesale cost'
    wholesale = float(input("Enter the item's wholesale cost: "))

    # Validate the wholesale cost.
    while wholesale < 0:
        print('ERROR: the cost cannot be negative.')
        wholesale = float(input('Enter the correct wholesale cost: '))

        #Calculate the retail price.
        retail = wholesale * mark_up

        #Display the retail price.
        print('Retail price: $', format(retail, ',.2f'), sep='')

        #Do this again.
        another = input('Do you have another item? ' + \
                        '(Enter y for yes): ')

HERE ARE THE RESULTS:

Enter the item's wholesale cost: 0.50
Enter the item's wholesale cost:    (THIS SEEMS TO BE A PROBLEM)
>>> ================================ RESTART ================================
>>> 
Enter the item's wholesale cost: -0.50  (YOU HAVE TO ENTER A NEGATIVE FIRST ???)  
ERROR: the cost cannot be negative.
Enter the correct wholesale cost: 0.50  
Retail price: $1.25
Do you have another item? (Enter y for yes): y
Enter the item's wholesale cost: 0.50
Enter the item's wholesale cost: 

 
THANK YOU FOR YOUR HELP.

Regards, Jag
BraveArt Multimedia 


More information about the Tutor mailing list