PYTHON program is refusing to loop
我将此代码粘贴在下面,基本上我正在尝试使其循环如果用户输入无效的测试分数(因为让我们面对它你不能有一个负面的测试分数或分数超过100%)。 所以基本上我试图让程序循环,如果他们确实输入了无效分数,直到他们输入一个在0到100(含)范围内的分数。
码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | A='A' B="B" C="C" D="D" F="F" score=float(input("Enter a test score:")) def determine_grade(score): while score >= 0 and score < 100: if score >=90 and score <=100: print(A) elif score >=80 and score <=89: print(B) elif score >=70 and score <=79: print(C) elif score >=60 and score <=69: print(D) elif score < 60: print(F) return score score = float(input("Invalid score range, please input it again:")) determine_grade(score) |
到目前为止我的输出看起来像这样:
Enter a test score: -2
Invalid score range, please input it again: -2
然后它停在那里,我需要它继续循环,直到我得到一个介于0和100(含)之间的值
你在while循环中有详细的测试。然后while循环应用于获得有效分数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | score=float(input("Enter a test score:")) def determine_grade(score): while score < 0 or score > 100: score = float(input("Invalid score range, please input it again:")) if score >=90 and score <=100: print('A') elif score >=80 and score <=89: print('B') elif score >=70 and score <=79: print('C') elif score >=60 and score <=69: print('D') elif score < 60: print('F') return score determine_grade(score) |
编辑:
让我们四处移动(又称重构)
让我们将主程序语句一起移动:
1 2 3 4 5 | def enter_and_validate_test_score(): score = float(input("Enter a test score:")) determine_grade(score) enter_and_validate_test_score() |
但是
该程序应首先获得有效分数,然后根据该分数打印分数:
1 2 3 4 | def enter_and_validate_test_score(): score = float(input("Enter a test score:")) score = validate_score(score) print_grade(score) |
这给我们留下了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | def validate_score(score): while score < 0 or score > 100: score = float(input("Invalid score range, please input it again:")) return score def determine_grade(score): if score >= 90 and score <= 100: print('A') elif score >= 80 and score <= 89: print('B') elif score >= 70 and score <= 79: print('C') elif score >= 60 and score <= 69: print('D') else: print('F') |
还要注意最后的
所以最后整个程序是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | def validate_score(score): while score < 0 or score > 100: score = float(input("Invalid score range, please input it again:")) return score def determine_grade(score): if score >= 90 and score <= 100: print('A') elif score >= 80 and score <= 89: print('B') elif score >= 70 and score <= 79: print('C') elif score >= 60 and score <= 69: print('D') else: print('F') def enter_and_validate_test_score(): score = float(input("Enter a test score:")) score = validate_score(score) print_grade(score) enter_and_validate_test_score() |
码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | A='A' B="B" C="C" D="D" F="F" def determine_grade(): while 1: try: score = float(raw_input("Enter score:")) if score < 0 or score > 100: print"Number in between 0 and 100" else: break except ValueError: print"Wrong input. Only float numbers" if score >=90 and score <=100: print(A) elif score >=80 and score <=89: print(B) elif score >=70 and score <=79: print(C) elif score >=60 and score <=69: print(D) elif score < 60: print(F) return score determine_grade() |
输出:
1 2 3 4 5 6 7 8 9 | vivek@vivek:~/Desktop/stackoverflow$ python 28.py Enter score: we Wrong input. Only float numbers Enter score: -8 Number in between 0 and 100 Enter score: 101 Number in between 0 and 100 Enter score: 56 F |