我返回了变量,我仍然得到变量仍然未定义。有人可以帮忙吗?在Python中的另一个函数中的函数中使用变量
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)' – falsetru