我返回了變量,我仍然得到變量仍然未定義。有人可以幫忙嗎?在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