gpt4 book ai didi

python - 如何修复“Python 返回方法不返回串联字符串中的整数

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:48 25 4
gpt4 key购买 nike

当代码运行时它会输出,但不是按要求输出,它会在所有百分比上返回空白

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: ', '')

我错过了什么?

最佳答案

您面临的问题是双重的。

raw_input 的输入类型是字符串,不会出错,因为

print "tata" * 2     # tatatata

是一个有效的字符串表示法 - 你一直乘以 0,所以你得到空字符串:''

其次,您需要调整除法以返回 float 。

固定代码:

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)

您将得到 float 不准确 - 请参阅 Is floating point math broken? - 您可以使用 round(3.2222,2) 舍入到 2 位小数。

关于python - 如何修复“Python 返回方法不返回串联字符串中的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54188923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com