简体   繁体   中英

Homework: Sales Tax python program

I'm working on a homework problem in Python. I'm trying to create a program that calculates sales tax. I need to give the user an option to set the beginning rate to calculate and an ending rate to calculate. The program will then calculate all whole numbers between begin and end (ex. begin =5%, end = 8% it would calculate 5,6,7,8 % tax.

I've been asked to use this code:

while begin <= 0 or begin > 10:
    begin = int(input("Enter tax rate: "))

I also have to use a for loop. I have to give the user 3 prompts: sales price, beginning rate, ending rate. The program will then give the user a table of rates and total prices:

I'm stumped at the for loop and incorporating the while statement

I'm at a very beginning process of:

productprice = float(input("Enter the product price: "))
begin = float(input("Enter tax rate: "))

total = productprice + productprice * begin/100
print total
raw_input ("Enter to exit")

Like this?

price = float(input("Enter the product price: "))

# declare begin and end variables as empty strings to start
begin = ""
end = ""

# the while loops are to make sure the user inputs a tax rate that 
# is more than 0 and less than 10
while begin <= 0 or begin > 10:
    begin = int(input("Enter tax rate: "))

while end <= 0 or end > 10:
    end = int(input("Enter end tax rate: "))    

# does the math for every integer between beginning and end, prints a 'table'   
for x in range(begin,end+1):
    total = price + price * x/100
    print "Price: "+str(price)+"\tTax: "+str(x)+"\tTotal: "+str(total)
    x += 1


raw_input ("Enter to exit")

Output:

Enter the product price: 100
Enter tax rate: 6
Enter end tax rate: 9
Price: 100.0    Tax: 6  Total: 106.0
Price: 100.0    Tax: 7  Total: 107.0
Price: 100.0    Tax: 8  Total: 108.0
Price: 100.0    Tax: 9  Total: 109.0
Enter to exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM