Discussion:
[Tutor] Python program malfunction
Jag Sherrington
2015-05-04 04:26:11 UTC
Permalink
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
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-05-04 07:34:29 UTC
Permalink
Post by Jag Sherrington
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.
#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.
#Get the item 's wholesale cost'
wholesale = float(input("Enter the item's wholesale cost: "))
# Validate the wholesale cost.
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?
Post by Jag Sherrington
#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): ')
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
THANK YOU FOR YOUR HELP.
Regards, Jag
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-05-04 07:31:32 UTC
Permalink
Post by Jag Sherrington
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.
#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.
That's the same test twice. Is that what you meant?
Post by Jag Sherrington
#Get the item 's wholesale cost'
wholesale = float(input("Enter the item's wholesale cost: "))
# Validate the wholesale cost.
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): ')
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Jag Sherrington
2015-05-04 23:08:13 UTC
Permalink
Hi, Alan> Enter the item's wholesale cost: 0.50 (AFTER THIS LINE PRINTS I HIT ENTER AND WOULD EXPECT THE NEXT LINE TO GIVE ME THE RESULT> Enter the item's wholesale cost:    "Retail price: $1.25"  INSTEAD WHEN I HIT ENTER I GET "Enter the item's wholesale cost: " AGAIN AND AGAIN
Regards, Jag
BraveArt Multimedia
Post by Jag Sherrington
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.
#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.
That's the same test twice. Is that what you meant?
Post by Jag Sherrington
      #Get the item 's wholesale cost'
      wholesale = float(input("Enter the item's wholesale cost: "))
      # Validate the wholesale cost.
          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): ')
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


_______________________________________________
Tutor maillist  -  ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail
Alan Gauld
2015-05-04 23:31:23 UTC
Permalink
Post by Jag Sherrington
Hi, Alan
Post by Jag Sherrington
Enter the item's wholesale cost: 0.50
(AFTER THIS LINE PRINTS I HIT ENTER AND WOULD
EXPECT THE NEXT LINE TO GIVE ME THE RESULT
But why would it? Your print code is all inside a loop that
only runs if the price is negative. If you enter a positive
price that code will not execute.

Look at your code. Follow it through line by line.
Post by Jag Sherrington
Post by Jag Sherrington
wholesale = float(input("Enter the item's wholesale cost: "))
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): ')
I suspect that if you look at your books code the lines starting
#Calculate the retail price.
are all aligned under the first while not the second?
--
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

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Dave Angel
2015-05-04 23:42:06 UTC
Permalink
Post by Jag Sherrington
Hi, Alan>
Please don't top-post. Enter your new message *after* whatever portion
of the previous message you're quoting. I'm rearranging the portion of
your message to conform to that standard.
Post by Jag Sherrington
Post by Jag Sherrington
# Process one or more items.
That's the same test twice. Is that what you meant?
Post by Jag Sherrington
#Get the item 's wholesale cost'
wholesale = float(input("Enter the item's wholesale cost: "))
# Validate the wholesale cost.
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): ')
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?
Enter the item's wholesale cost: 0.50 (AFTER THIS LINE PRINTS I HIT
ENTER AND WOULD EXPECT THE NEXT LINE TO GIVE ME THE RESULT>
Enter the item's wholesale cost: "Retail price: $1.25"
INSTEAD WHEN I HIT ENTER I GET "Enter the item's
wholesale cost: " AGAIN AND AGAIN
(See Peter's comment as well as Alan's second one.)

All the rest of your logic is inside the while loop that's only run when
the wholesale < 0 branch is taken. So you don't see any output from it,
as it never runs unless you enter a negative value followed by a
positive one.

You have to fix the indentation of the lines starting "#Calculate the
retail price"
--
DaveA
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...