简体   繁体   中英

Print statement is executed thrice, while execution only runs once

my code works just fine in terms of what I want it to print, but somehow it prints fs three times. So instead of for exampling printing A just once, it prints A tree times, each on a separate line. Why?

sr = input("Enter score: ")
for i in (sr):
    try:
        fs = float(sr)
        if fs > 1.0: print("decimal only")
        elif fs >= 0.9: print("A")
        elif fs >= 0.8: print("B")
        elif fs >= 0.7: print("C")
        elif fs >= 0.6: print("D")
        elif fs < 0.6: print("F")
    except:
        print("Bad score")
        sr = input("Enter score: ")
        continue

If user input is for example 0.8, it should just print B.

Right now you are not processing user input, therefore when you for loop over a string, you loop over the letters one by one therefore one for each ".08", You should process your input by first splitting by spaces (if you are expecting multiple inputs separated by spaces) and cast them to an integer.

It is not clear why you want to for loop over your input, if you are expecting a single input there is no need, if you want multiple inputs you must decide if you want them all input before processing or one at a time ( which would require a while loop).

sr = input("Enter score: ")
sr = sr.split(' ')

for i in (sr):
    i = int(i)
    try:
        fs = float(sr)
        if fs > 1.0: print("decimal only")
        elif fs >= 0.9: print("A")
        elif fs >= 0.8: print("B")
        elif fs >= 0.7: print("C")
        elif fs >= 0.6: print("D")
        elif fs < 0.6: print("F")
    except:
        print("Bad score")
        sr = input("Enter score: ")
        continue

There's no need to use a for-loop unless you want the program to run continuously after evaluating the user input. If you do then just adding a while-loop would work.

while True:
    try:
        sr = input("Enter score: ")
        fs = float(sr)
        if fs > 1.0: print("decimal only")
        elif fs >= 0.9: print("A")
        elif fs >= 0.8: print("B")
        elif fs >= 0.7: print("C")
        elif fs >= 0.6: print("D")
        elif fs < 0.6: print("F")
    except:
        print("Bad score")
        sr = input("Enter score: ")

Why are u typing : - for i in (sr): #

This will lead to loopind till ur digits example for 0.8 - three times for 0.88 - Four times for 0.888 - Five times instead do this kind of stuffs.

sr = input("Enter score: ")
if sr>=0 :
    try:
        fs = float(sr)
        if fs > 1.0: print("decimal only")
        elif fs >= 0.9: print("A")
        elif fs >= 0.8: print("B")
        elif fs >= 0.7: print("C")
        elif fs >= 0.6: print("D")
        elif fs < 0.6: print("F")
    except:
        print("Bad score")
        sr = input("Enter score: ")
        continue

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