代码运行时会输出,但不是必需的,它会在所有百分比上返回空白
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
预期结果是每个方法在调用时都应输出百分比。 但这就是所得到的。
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: ', '')
我想念什么?
答案 0 :(得分:2)
代码非常清楚:income
返回一个字符串。您在字符串上的“算术”将返回一个空字符串。返回之前将其转换为int
。另外,不要重载变量income
:
def get_income():
num = int(raw_input("Enter your Income: "))
print "Your income is: %d " % num
return num
income = get_income()
答案 1 :(得分:2)
您面临的问题是双重的。
使用raw_input的输入类型为字符串,不会出现错误,因为
print "tata" * 2 # tatatata
是有效的字符串表示法-您一直都乘以0,所以得到空字符串:''
。
第二,您需要调整除法以返回浮点数。
固定代码:
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
输出:
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)
您将获得浮点数错误-请参见Is floating point math broken?-您可以使用round(3.2222,2)
舍入到两位小数。
答案 2 :(得分:0)
raw_input在这里返回一个字符串,而不是整数,因此您必须将income变量转换为int格式,并在无法转换时进行错误处理。
由于收入是一个字符串,所以。此处的细微错误在于乘法。在python2中,整数除法返回舍入的整数。这意味着55/100将返回0,而不是0.55。您需要进行浮点除法,因此55.0 // 100将为您提供.55。
本质上,您要将收入输入(字符串)乘以0。在python中,字符串乘以值x会使字符串重复x次。因此,您在此处得到一个空白字符串。
答案 3 :(得分:0)
只需将收入返回为整数
return int(num)