[Tutor] Python program malfunction
Peter Otten
__peter__ at web.de
Mon May 4 09:34:29 CEST 2015
Jag Sherrington wrote:
> 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: '))
Look at the indentation of the rest of your code. To which while loop do you
want it to belong and in which while loop is it actually executed?
> #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
More information about the Tutor
mailing list