What i'm trying to do is have an initial input take a number, then proceed to take numbers that are entered afterwards until the loop is closed by entering 0. The output should be the Initial input, the amount entered added up, then subtracted from the Initial number.
I want to change the overall structure of the program as little as possible.
budget = float(input('Enter amount budgeted for the month: '))
spent = 0
total = 0
while spent >= 0:
spent = float(input('Enter an amount spent(0 to quit): '))
total += spent
print ('Budgeted: $', format(budget, '.2f'))
print ('Spent: $', format(total, '.2f'))
if budget > total:
difference = budget - total
print ('You are $', format(difference, '.2f'), \
'under budget. WELL DONE!')
elif budget < total:
difference = total - budget
print ('You are $', format(difference, '.2f'), \
'over budget. PLAN BETTER NEXT TIME!')
elif budget == total:
print ('Spending matches budget. GOOD PLANNING!')
First, you need to loop until user enters 0. You can use a loop that breaks on 0:
while True:
spent = float(input('Enter an amount spent(0 to quit): '))
if spent == 0: break
total += spent
Or loop until spent is 0. This means initializing it to some non-zero value.
spent = -1
while spent != 0:
spent = float(input('Enter an amount spent(0 to quit): '))
total += spent
Also, all the other code should be outside the loop:
budget = float(input('Enter amount budgeted for the month: '))
spent = -1
total = 0
while spent != 0:
spent = float(input('Enter an amount spent(0 to quit): '))
total += spent
print ('Budgeted: $', format(budget, '.2f'))
print ('Spent: $', format(total, '.2f'))
if budget > total:
difference = budget - total
print ('You are $', format(difference, '.2f'), \
'under budget. WELL DONE!')
elif budget < total:
difference = total - budget
print ('You are $', format(difference, '.2f'), \
'over budget. PLAN BETTER NEXT TIME!')
else:
print ('Spending matches budget. GOOD PLANNING!')
Related
I want to create a while loop to prompt the user if they would like to enter a living cost. The user would input 'y' for yes and the loop would proceed.
I this loop I would like to add up all the living expense entered and once the loop ends to store to the total amount in total_living.
Example would be
l_cost = input('Enter living cost? y or n ')
while l_cost != 'n' (loop for living cost)
totat_living = (keeps adding until I say all done
l_cost = input('Enter living cost? y or n ')
Other while and for loops for different scenarios
total_exp = total_living + total_credit + total_debt ect ect
I just need a little help with this as to how to add up multiple values and then maintaining the total value for the loop that I am in.
If someone could point me to an example of a function or loop that is similar or tell me where to look that would be great!
You may try using while as below:
flag = input ("wish to enter cost; y or n" )
total=0
While flag != 'n':.
cost = int(input ("enter cost: "))
total = total + cost
flag = input("more ?? Y or n")
print(total)
total_cost = 0.0
prompt = 'Enter a living cost? y or n: '
answer = input(prompt)
while answer.lower() != 'n':
cost = input('Enter your cost: ')
total_cost = total_cost + float(cost)
answer = input(prompt)
print('Total cost is $' + str(total_cost))
def checkout():
total = 0
count = 0
moreItems = True
while moreItems:
price = float(input('Enter price of item (0 when done): '))
if price != 0:
count = count + 1
total = total + price
print('Subtotal: $', total)
else:
moreItems = False
average = total / count
print('Total items:', count)
print('Total $', total)
print('Average price per item: $', average)
checkout()
Note
Try to convert this example to your problem.
I am trying to recreate a problem from our classwork: Write a program that can handle a shopping event. First, it requests the number of items bought, then asks for each items' price and tax rate. Then prints the total cost.
Example:
How many items you bought: 2
For item 1
Enter the price: 10
Enter the tax rate: 0
For item 2
Enter the price: 20
Enter the tax rate: 8
Your total price is 31.6
I have no knowledge on how I would compute for items larger than 1. ie my code works for 1 item.
items = int(input("How many items did you buy? "))
for i in range(1, items+1, 1):
print("For item ",i)
price = float(input("Enter the price: "))
tax_rate = float(input("Enter the tax rate: "))
total = price + price*(tax_rate/100)
print("Your total price is", total)
I need to somehow save the totals after each iteration and add them all up. I am stumped.
Note: This is an introduction to python course and also my first programming course. We've only learned for loops thus far.
You need to have an initialized counter to have a running total.
items = int(input("How many items did you buy? "))
total = 0
for i in range(1, items+1, 1):
print("For item ",i)
price = float(input("Enter the price: "))
tax_rate = float(input("Enter the tax rate: "))
total += price + price*(tax_rate/100)
print("Your total price is", total)
I wanted the result to show the total amount of money after adding interest every year but it only increases the year but not the amount. Why?
while True:
try:
investment = float(input('How much to invest : '))
interest = float(input('Interest rate : '))
break
except ValueError:
"Please enter a valid number"
for year in range(10):
money = investment + (investment * interest)
print("Total money in year {} : {}".format((year+1), money))
It sounds like you need to accrue the interest:
for year in range(10):
investment += (investment * interest)
print("Total money in year {} : {}".format((year + 1), investment))
Logical error. Your investment variable does not be assigned each round in the loop.
I'm working on this simple task where I need to use 2 while loops. The first while loop checks if the number of hours is less than 0 if it is then loop should keep asking the user.
Here's my code:
hours = float(input('Enter the hours worked this week: '))
count = 0
while (0 > hours):
print ('Enter the hours worked this week: ')
count = count + 1
pay = float(input('Enter the hourly pay rate: '))
while (0 > pay):
print ('Enter the hourly pay rate: ')
count = count + 1
total_pay = hours * pay
print('Total pay: $', format(total_pay, ',.2f'))
break is what you're looking for.
x = 100
while(True):
if x <= 0:
break
x -= 1
print x # => 0
As for your example, there is nothing that would seem to cause a break to occur. For example:
hours = float(input('Enter the hours worked this week: '))
count = 0
while (0 > hours):
print ('Enter the hours worked this week: ')
count = count + 1
You are not editing the hours variable at all. This would merely continue to print out "Enter the hours worked this week: " and increment count ad infinitum. We would need to know what the goal is to provide any more help.
You exit a loop by either using break or making the condition false.
In your case, you take input from the user, and if hours < 0, you print the prompt and update the count, but you don't update hours.
while (0 > hours):
print ('Enter the hours worked this week: ')
count = count + 1
should be:
while (0 > hours):
hours = float(input('Enter the hours worked this week: '))
count = count + 1
Similarly for pay:
while (0 > pay):
pay = float(input('Enter the hourly pay rate: '))
count = count + 1
Well, the other answer shows you how to break out of a while loop, but you're also not assigning to the pay and hours variables. You can use the built-in input function to get what the user supplied into into your program
hours = float(input('Enter the hours worked this week: '))
count = 0
while (0 > hours):
hours = input('Enter the hours worked this week: ')
count = count + 1
pay = float(input('Enter the hourly pay rate: '))
while (0 > pay):
pay = input('Enter the hourly pay rate: ')
count = count + 1
total_pay = hours * pay
print('Total pay: $', format(total_pay, ',.2f'))
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