当代码运行时,它会输出,但不是按要求输出,它会在所有百分比上返回空白
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: ', '')
我错过了什么?你知道吗
原始输入在这里返回的是一个字符串,而不是整数,因此您必须将income变量转换为int,并且在无法转换时返回错误句柄。你知道吗
因为收入是一根弦。这里的细微错误是乘法。在python2中,整数除法返回向下舍入的整数。这意味着55/100将返回0,而不是0.55。你需要浮点除法,所以55.0//100会给你.55。你知道吗
基本上,你的收入输入(一个字符串)乘以0。在python中,字符串乘以值x会将字符串重复x次。所以,这里有一个空白字符串。你知道吗
代码非常清楚:
income
返回一个字符串。您对字符串的“算术”返回一个空字符串。在返回之前将其转换为int
。另外,不要重载变量income
:你面临的问题是双重的。你知道吗
原始输入的输入类型是字符串,没有错误,因为
是一个有效的字符串表示法-一直乘以0,因此得到空字符串:
''
。你知道吗其次,需要调整除法以返回浮点值。你知道吗
固定代码:
输出:
您将得到浮点不准确性-请参见Is floating point math broken?-您可以使用
round(3.2222,2)
四舍五入到2位小数。你知道吗相关问题 更多 >
编程相关推荐