我正在研究Python中的家庭作业问题。我正在尝试创建一个计算销售税的计划。我需要为用户提供一个选项来设置要计算的开始率和要计算的结束率。然后程序将计算开始和结束之间的所有整数(例如,开始= 5%,结束= 8%,它将计算5,6,7,8%的税。
我被要求使用此代码:
while begin <= 0 or begin > 10:
begin = int(input("Enter tax rate: "))
我还必须使用for循环。 我必须给用户3提示:销售价格,开始率,结束率。然后,该程序将为用户提供费率和总价格表:
我被困在for循环中并加入了while语句
我正处于一个刚刚开始的过程中:
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")
答案 0 :(得分:1)
喜欢这个吗?
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")
输出:
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