如何修复“ Python Return 方法不返回串联字符串中的整数
How to fix " Python Return Method not returning the integer in a concatenated string
当代码为 运行 时,它会输出,但未按要求输出,它 returns 在所有百分比上都是空白的
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
returns一个字符串。你的 "arithmetic" 上的字符串 returns 是一个空字符串。返回前将其转换为 int
。另外,不要重载变量 income
:
def get_income():
num = int(raw_input("Enter your Income: "))
print "Your income is: %d " % num
return num
income = get_income()
您面临的问题是双重的。
你输入的 raw_input 的类型是字符串,你没有得到错误,因为
print "tata" * 2 # tatatata
是一个有效的字符串表示法 - 你一直乘以 0,所以你得到空字符串:''
.
其次,您需要将除法调整为 return 浮点数。
固定码:
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 位小数。
raw_input这里返回的是一个字符串,不是一个整数,所以你必须将income变量转换成一个int,不能转换时的错误处理。
由于收入是一个字符串,.这里的细微错误在于乘法。在python2中,整数除法returns四舍五入的整数。这意味着 55/100 将返回 0,而不是 0.55。你需要浮点数除法,所以 55.0//100 会给你 .55.
本质上,您是将收入输入(字符串)乘以 0。字符串乘以 python 中的值 x 会重复字符串 x 次。所以,你在这里得到一个空字符串。
只是return作为int的收入
return int(num)
当代码为 运行 时,它会输出,但未按要求输出,它 returns 在所有百分比上都是空白的
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
returns一个字符串。你的 "arithmetic" 上的字符串 returns 是一个空字符串。返回前将其转换为 int
。另外,不要重载变量 income
:
def get_income():
num = int(raw_input("Enter your Income: "))
print "Your income is: %d " % num
return num
income = get_income()
您面临的问题是双重的。
你输入的 raw_input 的类型是字符串,你没有得到错误,因为
print "tata" * 2 # tatatata
是一个有效的字符串表示法 - 你一直乘以 0,所以你得到空字符串:''
.
其次,您需要将除法调整为 return 浮点数。
固定码:
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 位小数。
raw_input这里返回的是一个字符串,不是一个整数,所以你必须将income变量转换成一个int,不能转换时的错误处理。
由于收入是一个字符串,.这里的细微错误在于乘法。在python2中,整数除法returns四舍五入的整数。这意味着 55/100 将返回 0,而不是 0.55。你需要浮点数除法,所以 55.0//100 会给你 .55.
本质上,您是将收入输入(字符串)乘以 0。字符串乘以 python 中的值 x 会重复字符串 x 次。所以,你在这里得到一个空字符串。
只是return作为int的收入
return int(num)