I ask the user in my program to enter up to four types of income, with 0 filling the unused income slots. Then, the program is supposed to add up the incomes. It goes like this:

user defines values for incomes 1, 2, 3 and 4.
Program adds together the values of incomes 1, 2, 3 and 4.
Then it prints the calculation result to the screen.
Instead, it just repeats the variable that the user sets for incomes 1, 2, 3 and 4, and prints them in a long string, so if the values were each set to 500, it would print:

500500500500.
What am I doing wrong?

Here is the source code:

def clear():
    print """













"""
def Main():
    x = raw_input("# ")
    if x == "calc" or x == "calculate":
        calculate()
    elif x == "convert":
        clear()
        print "Convert not implemented yet."
        

def calculate():
    x = raw_input("Choose calculation type: ")
    if x == 'money':
        money_calc()
    elif x == 'budget':
        budget_calc()
    elif x == 'grades':
        grades_calc()
    else:
        clear()
        print """Your only options within the calculate function are:

                    'money' , 'grades', and 'budget'.

                    """
        calculate()

def money_calc():
    clear()
    print """Choose your calculation type:

        a. adding income      b. adding expenses  c. calculate total left over income after expenses
        """
    
    x = raw_input("Please choose a, b, or c: ")
    if x == 'a' or x == 'a.':
        clear()
        add_income()
    elif x == 'b' or x == 'b.':
        clear()
        add_expenses()
    elif x == 'c' or x == 'c.':
        clear()
        total_after_expenses()
    else:
        clear()
        print "You must choose one of the following:"
        money_calc()

def add_income():
    clear()
    x0 = raw_input("Enter Income 1: ")
    x1 = raw_input("Enter income 2: ")
    x2 = raw_input("Enter Income 3: ")
    x3 = raw_input("Enter Income 4: ")

    print "We will now calculate your total income amount."

    calc = [x0 + x1 + x2 + x3]
    print calc
    x = raw_input("Press enter to go main prompt menu... ")
    if x == "":
        clear()
        Main()
    else:
        clear()
        Main()
        
        
        

Main()

Recommended Answers

All 3 Replies

What's happening is that you aren't adding the numbers; you're concatenating strings. In python, "hello" + "world" = "helloworld".

To get what you want, convert the input to integers using int()

Scru just posted the same thing as I was (well except that I would use floats, unless income is whole dollars only), so I will submit an idea to simplify entry.

x = []    ##  will contain the entries
total = 0
for j in range(1, 5):
   income = float(raw_input("Enter Income %d: " % (j)))
   x.append( income )
   total += income
# Note that total is a float, not a list.
print "total income is %9.2f" % (total)

Also -
You do not need the square brackets [] in line 76 unless you actually need for the total income to be a list. While it will not affect the result, the answer will be printed as a list instead of a string and might look a little odd.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.