I keep getting a error that tuple cant be subtracted from a integer. I am trying to add all inputs to a list, then add all contents together and subtract it from the total monthly cash flow.
def budget_50_total():
list=[]
monthly = int(input("How much is your monthly cash flow "))
essential = budget_50(monthly)
print(essential)
# Do calculation for 50%
print("Please enter your essential expenses! ")
house = int(input("How much is your housing for month: "))
utilities = int(input("How much is your utilities this month 'gas, power, etc': "))
grocery = int(input("How much is your groceries for month "))
health = int(input("How much is your health insurance for the month "))
car = int(input("How much is your car payment"))
for i in range(1):
data = house, utilities,grocery,health,car
list.append(data)
print(list)
total = sum(list)
print(total)
total2 = essential - total
print(total2)
This line creates a tuple:
data = house, utilities,grocery,health,car
Then you add the tuple to the list in the next line. You won't be able to sum a list of tuple. You could however do
data = house, utilities,grocery,health,car
print(data)
total = sum(data)
However, your error "tuple cant be subtracted from a integer" is likely caused by your calculation for total2 as that is your only subtraction in the code shown. It's impossible to say what the issue is here without knowing what essential is.
You can use a for loop to add the sum of each tuple in list to the total:
total = 0
for x in list:
total += sum(x)
Related
how_many_number = int(input("How many number do you want to print? "))
for take_number in how_many_number:
take_number = int(input("Enter number: "))
sum = 0
sum = sum + take_number
print(sum)
Here you go. To take user input we can use a for loop and for each iteration we can add it to our sum using += operator. You can read through the following code to understand it well enough.
number_of_inputs = int(input("How many number to sum: ")
sum = 0 # Initialize the sum variable
for x in range(number_of_inputs): # Repeat the number of inputs times
sum += int(input("Enter Value: ")) # Take a input and add it to the sum
print("Sum is", sum) # print out the sum after completing
You can also compress it into a List Comprehension, like this...
how_many_number = int(input("How many number do you want to print? "))
print(sum([int(input("Enter number: ")) for i in range(how_many_number)]))
i would use the following, note: error handle is not yet incorporated.
num_list = []
### create a function that adds all the numbers entered by a user
### and returns the total sum, max 3 numbers
### return functions returns the entered variables
def add_nums():
while len(num_list) < 3:
user_num = int(input('please enter a number to add:'))
num_list.append(user_num)
print('You have entered the following numbers: ',num_list)
total_num_sum = 0
for x in num_list:
total_num_sum += x
print('total sum of numbers = ',total_num_sum)
add_nums()
Also, please follow the StackOverFlow post guidelines; have your post title as a problem question, it has to be interesting for other devs, and add more emphasis on why you want to add numbers entered by user vs running calc on a existing or new data-frame, need more meat.
I created a program that calculates how much money is saved after a "percentage off" is applied. The user is able to input the price of the product as well as the percentage off. After the program does the calculation, how do I continuously loop the program so that the user can keep using it without me having to click "run" each time in the console.
def percent_off_price():
price = amount * percent
amount = float(input('Enter the price of your product: '))
percent = float(input('Enter the percentage off: '))
price = amount * percent
print('You will save $',price,'off! Your total is: $',amount - price,)
Put the whole code segment in a while loop which runs always but it is the worst thing you would want:
while True:
amount = float(input('Enter the price of your product: '))
percent = float(input('Enter the percentage off: '))
price = amount * percent
print('You will save $',price,'off! Your total is: $',amount - price,)
You can also put a check to run it for controlled amount. Hence it would only run if the amount entered is a positive value:
flag= True
while flag:
amount = float(input('Enter the price of your product: '))
if amount <0:
flag=False
percent = float(input('Enter the percentage off: '))
price = amount * percent
print('You will save $',price,'off! Your total is: $',amount - price,)
Lastly if you want to run it for a fixed no. of times (Let's say 10) you can go for a for loop:
for i in range(10):
amount = float(input('Enter the price of your product: '))
percent = float(input('Enter the percentage off: '))
price = amount * percent
print('You will save $',price,'off! Your total is: $',amount - price,)
You can do:
while True:
percent_off_price()
code:
if item_id == 1:
price = int(500)
Quantity = int(input("How much do you want?"))
total = Quantity * price
if total < money:
print("You have successfully bought this item.")
money - total
Just saying, if this helps, I use repl.it, an online code environment.
It looks like you want to update money, so just reassign the result of the subtraction back to money.
money = money - total
Python has a shortcut operator you could use instead.
money -= total
New to Python, doing Introduction to Programming with Python with Grok Learning.
I have this problem where I need to take input, convert to a list, convert to integers and then collect the sum of the integers. Here's what I have so far:
expenses = input("Enter the expenses: ")
expenses.split()
for expense in expenses:
print(int(expenses))
total = sum(expenses)
print("Total: $" + total)
I was told I have to loop over the array and then convert to integers. But I have no idea what this means, can someone please show me?
Since you already wrote that for loop I'm assuming you know what it means, so you simply need to create another list, for storing the int values:
intValues = []
for expense in expenses:
intValues.append(int(expense))
and then print(sum(intValues)) works the same. You could do the same in just one line using Python's list comprehension syntax:
intValues = [int(expense) for expense in expenses]
Firstly you need to indent total=sum(expenses) into the for loop and need to save the split result in a variable so modified program is:
expenses = input("Enter the expenses: ")
for expense in expenses.split:
print(int(expense))
total = sum(expense)
print("Total: $" + total)
Try this:
expenses = input('Enter the expenses: ')
expenses = expenses.split()
total = 0
for expense in expenses:
total += int(expense)
print('Total: $' + str(total))
When I did this challenge, this was my code:
money = input("Enter the expenses: ")
money = money.split()
total = sum([int(i) for i in money ])
print("Total:", "$" + str(total))
The first line is just the input of money. The second line splits each number into a list. The 3rd line calculates the sum of the input as an integer, then the 4th changes it back to a string and prints it.
To fix your error, try this:
expenses = input("Enter the expenses (separated by spaces): ")
total = 0
for expense in expenses.split():
total += int(expense)
print(expense)
print( "Total: $" + str(total) )
A sample session:
Enter the expenses (separated by spaces): 12 34 56
12
34
56
Total: $102
def getExamPoints(examPoints):
for examPoints in range(1, 5):
examPoints = input("Please enter students exam scores: ")
totalPoints = input("Please enter total possible points: ")
print("The total exam points are: " + sum(int(examPoints)))
avg = float(int(str(examPoints))/int(totalPoints))
print("the average is: ", avg)
on Line 5 I am getting the error 'int object is not iterable'
and I have no idea why.
I am attempting to write a program with functions and this portion of the function is suppose to take four homework scores each out of eighty points and calculate the average of the scores and then take that average and multiply it by the percentage that homework is worth for the class, but I cant even seem to get this chunk of the program to get an average of homework scores. I am not very good with python, also if this isn't formatted correctly I apologize in advance, but any help would be much appreciated.
examPoints is not a list of inputs in the original code, but just one variable that gets overwritten with each iteration of the user-input loop:
for examPoints in range(1, 5):
examPoints = input("Please enter students exam scores: ")
Instead, you want to keep each input separately.
e.g. by appending it to a list:
examPoints = []
for _ in range(1,5):
# add input to list after converting it to an integer
examPoints.append(int(input("Please enter students exam scores: ")))
...
The input-text-to-integer conversion can be done either as you are appending (return error to user immediately upon input that can't be converted), or when you're performing the sum, by using a list comprehension or the map function:
# sum version
sum([int(v) for v in examPoints])
# map version
sum(map(int, examPoints))
Sorry, but (In my opinion) your code is a bit messy. Instead, try:
def getExamPoints(examPoints):
points = []
for examPoints in range(1, 5):
points = points + [int(input("Please enter students exam scores: "))]
totalPoints = input("Please enter total possible points: ")
print("The total exam points are: " + sum(examPoints))
avg = float(int(str(examPoints))/int(totalPoints))
print("the average is: ", avg)
what sum() looks for is an iterable object, like a list, and adds together everything in it. Since examPoints is defined as an integer, it is not iterable. Instead, make a separate list, and put the input inside there.