python新手求助
写一个投票的代码
def vote()
有三种,yes,no,abstained可以填
都是yes 输出unanimously
三分之二yes输出super majority
二分之一yes输出simple majority
小于二分之一的yes输出fails
yes,no,abstained 大小写任意组合都要可以识别
图上是例子
def vote(stra):
yesstr=['yes','y']
nostr=['no','n']
abstainedstr=['abstained','a']
count=0
yescount=0
stra=stra.replace(',',' ')
for i in stra.split():
lowerstr=i.lower()
if lowerstr in yesstr:
yescount+=1
count+=1
elif lowerstr in nostr:
count+=1
if yescount==count:
return 'proposal passes unanimously'
if yescount*1.0/count>=2.0/3.0:
return 'proposal passes with super majority'
if yescount*1.0/count>=0.5:
return 'proposal passes with simple majority'
return 'proposal fails'
if __name__=='__main__':
stra=raw_input('Enter the yes,no,abstained votes one by one and the press enter:\n')
print vote(stra)
yesstr=['yes','y']
nostr=['no','n']
abstainedstr=['abstained','a']
count=0
yescount=0
stra=stra.replace(',',' ')
for i in stra.split():
lowerstr=i.lower()
if lowerstr in yesstr:
yescount+=1
count+=1
elif lowerstr in nostr:
count+=1
if yescount==count:
return 'proposal passes unanimously'
if yescount*1.0/count>=2.0/3.0:
return 'proposal passes with super majority'
if yescount*1.0/count>=0.5:
return 'proposal passes with simple majority'
return 'proposal fails'
if __name__=='__main__':
stra=raw_input('Enter the yes,no,abstained votes one by one and the press enter:\n')
print vote(stra)
温馨提示:答案为网友推荐,仅供参考