[Tutor] Python program malfunction
Alan Gauld
alan.gauld at btinternet.com
Mon May 4 09:31:32 CEST 2015
On 04/05/15 05:26, 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':
That's the same test twice. Is that what you meant?
> #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)
No problem, its exactly what your program tells it to do.
If the cost is >0 there is nothing else to do so it goes
round the loop a second time.
What did you expect it to do?
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list