1

When the code is run it outputs, but not as required, it returns blank on all the percentages

def income():
    num= raw_input("Enter your Income: ")
    print "Your income is: %d " % int(num)
    return num
income= income()

def bills_expenses():
    bills_kitty=0
    bills_kitty=(income*(55/100))
    return  ("Your bills kitty should have: ", bills_kitty)

def long_term_savings():
    long_term_savings_kitty=(10/100)*income
    return "Your long term kitty should have: ",  long_term_savings_kitty

def play_and_leisure():
    play_and_leisure_kitty=(10/100)*income
    return "Your play and leisure kitty should have: " , play_and_leisure_kitty


def education_personal_growth():
    education_personal_growth_kitty=(10/100)*income
    return "Your personal growth  kitty should have: ", education_personal_growth_kitty


def pay_myself_Financial_fredom():
    pay_myself_Financial_fredom_kitty=(10/100)*income
    return "Your pay yourself kitty should have: ", pay_myself_Financial_fredom_kitty

def give_back_to_the_community():
    give_back_to_the_community_kitty=(5/100)*income
    return "Your giving back kitty should have: " , give_back_to_the_community_kitty



bills=bills_expenses()
long_term = long_term_savings()
play=play_and_leisure()
education=education_personal_growth()
mine=pay_myself_Financial_fredom()
give=give_back_to_the_community()


print bills
print long_term
print play
print education
print mine
print give

The expected results are that each method when called should output the percentages. but this is what am getting.

Enter your Income: 200
Your income is: 200
('Your bills kitty should have: ', '')
('Your long term kitty should have: ', '')
('Your play and leisure kitty should have: ', '')
('Your personal growth  kitty should have: ', '')
('Your pay yourself kitty should have: ', '')
('Your giving back kitty should have: ', '')

What am I missing?

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
Wanjila Abraham
  • 521
  • 1
  • 7
  • 17

4 Answers4

2

The code is quite clear: income returns a string. Your "arithmetic" on the string returns an empty string. Convert it to int before returning. Also, do not overload the variable income:

def get_income():
    num = int(raw_input("Enter your Income: "))
    print "Your income is: %d " % num
    return num

income = get_income()
Prune
  • 76,765
  • 14
  • 60
  • 81
2

The problem you are facing is twofold.

The type of your input with raw_input is string, you get no error because

print "tata" * 2     # tatatata

is a valid string notation - you multiply by 0 all the time, so you get empty strings: ''.

Secondly you need to adjust the division to return floats.

Fixed code:

def get_income(): # See Prunes post
    num = float(raw_input("Enter your Income: "))
    print "Your income is: %d " % int(num)
    return num

income= get_income()

print(type(income))

def bills_expenses():
    bills_kitty=0
    bills_kitty=(income*(55/100.0))
    return  ("Your bills kitty should have: ", bills_kitty)

def long_term_savings():
    long_term_savings_kitty=(10/100.0)*income
    return "Your long term kitty should have: ",  long_term_savings_kitty

def play_and_leisure():
    play_and_leisure_kitty=(10/100.0)*income
    return "Your play and leisure kitty should have: " , play_and_leisure_kitty

def education_personal_growth():
    education_personal_growth_kitty=(10/100.0)*income
    return "Your personal growth  kitty should have: ", education_personal_growth_kitty

def pay_myself_Financial_fredom():
    pay_myself_Financial_fredom_kitty=(10/100.0)*income
    return "Your pay yourself kitty should have: ", pay_myself_Financial_fredom_kitty

def give_back_to_the_community():
    give_back_to_the_community_kitty=(5/100.0)*income
    return "Your giving back kitty should have: " , give_back_to_the_community_kitty

bills=bills_expenses()
long_term = long_term_savings()
play=play_and_leisure()
education=education_personal_growth()
mine=pay_myself_Financial_fredom()
give=give_back_to_the_community()


print bills
print long_term
print play
print education
print mine
print give

Output:

Enter your Income: Your income is: 200 
<type 'float'>
('Your bills kitty should have: ', 110.00000000000001)
('Your long term kitty should have: ', 20.0)
('Your play and leisure kitty should have: ', 20.0)
('Your personal growth  kitty should have: ', 20.0)
('Your pay yourself kitty should have: ', 20.0)
('Your giving back kitty should have: ', 10.0)

You will get float-inaccuracies - see Is floating point math broken? - you can use round(3.2222,2) to round to 2 decimal digits.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

raw_input is returning a string here, not an integer, so you'll have to cast the income variable to an int, and error handle when it cannot be cast.

Since income is a string, . The subtle error here is in the multiplication. In python2, integer division returns the rounded-down integer. This means 55/100 will give back 0, not 0.55. You need float division, so 55.0//100 will give you .55.

Essentially, you're multiplying your income input (a string) by 0. String multiplication by a value x in python repeats the string x times. So, you're getting a blank string here.

rohanphadte
  • 978
  • 6
  • 19
0

Just return the income as int

return int(num)
mad_
  • 8,121
  • 2
  • 25
  • 40