[英]How do I get the rest of the code to print?
我的代码只打印到第一组 else if 语句我如何让我的代码打印下一组 else if 语句。
player=input("Player name:")
hits=int(input("Hits:"))
ab=int(input("At Bats:"))
walks=int(input("Walks:"))
hbp=int(input("Hit By Pitch:"))
tb=int(input("Total Bases:"))
ba=hits/ab
obp=(hits+walks+hbp)/(ab+walks+hbp)
slg= tb/ab
iso= slg - ba
ops= obp + slg
print(player,"hit","{:.3f}".format(ba))
print(player,"On-base Percentage was","{:.3f}".format(obp))
print(player,"slugged","{:.3f}".format(slg))
print(player,"Had an isolated power of","{:.3f}".format(iso))
print(player,"Had on-base plus slugging od","{:.3f}".format(ops))
if ba >= .300:
print(player,"batting average is elite")
elif .299 >= ba >= .280:
print(player,"batting average is great")
elif .279 >= ba >= .260:
print(player,"batting average is above average")
elif .259 >= ba >= .250:
print(player,"batting average is average")
elif ba <= .249:
print(player,"batting average is below average")
if obp >= .390:
print(player,"On base percentage is elite")
elif .389 >= obp >= .370:
print(player,"On base percentage is great")
elif .269 >= obp >= .340:
print(player,"On base percentage is above average")
elif .339 >= obp >= .320:
print(player,"On base percentage is above average")
elif obp <= .319:
print(player,"On base percentage is below average")
这是运行时会发生什么
#input:
#Player name: jose
#Hits: 167
#At Bats:601
#Walks:66
#Hit By Pitch:4
#Total Bases:294
#output:
#jose hit 0.278
#jose On-base Percentage was 0.353
#jose slugged 0.489
#jose Had an isolated power of 0.211
#jose Had on-base plus slugging od 0.842
#jose batting average is above average
我希望程序打印下一条语句“玩家在基地百分比是 xxx”
elif .269 >= obp >= .340:
有一个错字,您可能想输入.369
,但这不是主要问题。
您缺少一些值。 你有
if obp >= .390:
...
elif .389 >= obp >= .370:
例如,如果值为.3895
会发生什么? 这是不到.390
但超过.389
所以它不符合任何条件。 您可能看不到这样的值,因为您使用字符串格式{:.3f}
隐藏它们。
修复方法是:
if obp >= .390:
print(player,"On base percentage is elite")
elif obp >= .370:
print(player,"On base percentage is great")
elif obp >= .340:
print(player,"On base percentage is above average")
elif obp >= .320:
print(player,"On base percentage is average")
else
print(player,"On base percentage is below average")
这是有效的,因为一旦找到true
条件并执行该案例的主体,它之后的所有其他条件都将被忽略 - 即使它们也是true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.