Python Forum
Homework help:While Loops question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework help:While Loops question
#1
I am doing this question for homework which says: Write a Grocery program that acts as a cash register. The program will prompt how many items they are buying. Then they will input the price of each item (these should be decimal numbers). The program will display SUBTOTAL, TAX (13%), and the TOTAL (with tax)(This is using the While Loop). Make sure you include $ signs and round to two decimal places.I have written the following code below, but the software keeps looping forever. Can someone help me figure why it keep looping

numItems = int(input("How many items are you buying?:"))

#Initializes the amount of items and the price
subPrice = 0
x = 0

#use conditional loop to loop until the price amount mathces with the number of items
while x in range (0,numItems):
    price = int(input("Enter in a price: $")) #program asks for the price:
    numItems = numItems + 1 # Counts how many items entered
    subPrice = subPrice + price # Calculates the sum

#Calculates the tax and total price
tax = subPrice * 0.13
tax2 = round (tax,2)
Total = subPrice - tax
total2 = round(total,2)

# prints out the Subtotal, tax and Total
print ("Subtotal: $", subPrice)
print ("Tax", tax2)
print ("Total", total2)
Reply
#2
x is always 0 since it doesn't change in the loop, so x in range(0, numItems) is always True. While loops don't work the same way as for loops.
Reply
#3
Thanks! So how can i fix the problem?
Reply
#4
Use for instead of while
Reply
#5
I have to do the program only using a while loop
Reply
#6
Then you will have to do the incrementing yourself instead of relying on "in range()".
Reply
#7
I'm not sure if this is correct, but How about using a break?
numItems = int(input("How many items are you buying?:"))
 
#Initializes the amount of items and the price
subPrice = 0
x = 0
 
#use conditional loop to loop until the price amount mathces with the number of items
while x in range (0,numItems):
    price = int(input("Enter in a price: $")) #program asks for the price:
    numItems = numItems + 1 # Counts how many items entered
    subPrice = subPrice + price # Calculates the sum
    break
 
#Calculates the tax and total price
tax = subPrice * 0.13
tax2 = round (tax,2)
Total = subPrice - tax
total2 = round(Total,2)
 
# prints out the Subtotal, tax and Total
print ("Subtotal: $", subPrice)
print ("Tax", tax2)
print ("Total", total2)
Note that on line 16, you have defined total with a capital T but in line 17, when you round Total, you don't use a capital T.
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question about loops rpang 7 1,596 Nov-07-2022, 09:42 PM
Last Post: deanhystad
  IF ELSE Homework Question buckthimble 1 2,147 Mar-29-2020, 06:29 AM
Last Post: buran
  Python Homework Question OrcDroid123 1 2,370 Sep-01-2019, 08:44 AM
Last Post: buran
  Homework question dmhhfm 4 13,354 Apr-10-2019, 07:22 AM
Last Post: DeaD_EyE
  Beginner Python Homework Question (Calculate Gross Pay) matchamochi7 4 5,679 Nov-02-2018, 01:06 PM
Last Post: buran
  yet another homework question HakolYahol 16 7,689 Sep-27-2018, 04:52 PM
Last Post: gruntfutuk
  python in JES homework question, lsteffen 1 3,004 Feb-11-2018, 05:52 PM
Last Post: Taco_Town

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020