简体   繁体   中英

How do I get the rest of the code to print?

My code is only printing up to the first set of else if statements how do I get it for my code to print the next set of 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")

This what happens when is run it

#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

I want the program to print the next statement "player on-base percentage is xxx"

There is a typo at elif .269 >= obp >= .340: , you probably meant to type .369 , but that is not the main issue.

You are missing some values. You have

if obp >= .390:
    ...
elif .389 >= obp >= .370:

What happens if the value is .3895 , for example? That's less than .390 but more than .389 so it doesn't meet any condition. You may not be seeing values like this because you are hiding them with the string format {:.3f} .

The fix is:

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")

This works because once a true condition is found and the body of that case is executed, all others after it will be ignored - even though they are also true .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM