【发布时间】:2016-01-16 21:11:56
【问题描述】:
我返回了变量,但我仍然得到变量仍未定义。有人可以帮忙吗?
def vote_percentage(s):
'''(string) = (float)
count the number of substrings 'yes' in
the string results and the number of substrings 'no' in the string
results, and it should return the percentage of "yes"
Precondition: String only contains yes, no, and abstained'''
s = s.lower()
s = s.strip()
yes = int(s.count("yes"))
no = int(s.count("no"))
percentage = yes / (no + yes)
return percentage
def vote(s):
##Calling function
vote_percentage(s)
if percentage == 1.0: ##problem runs here
print("The proposal passes unanimously.")
elif percentage >= (2/3) and percentage < 1.0:
print("The proposal passes with super majority.")
elif percentage < (2/3) and percentage >= .5:
print("The proposal passes with simple majority.")
else:
print("The proposal fails.")
【问题讨论】:
-
将返回值赋值给一个变量:
percentage = vote_percentage(s)
标签: python function return callable