[英]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.