我将此代码粘贴在下面,基本上我试图让它循环如果用户输入无效的测试分数(因为让我们面对它你不能有一个负面的测试分数或分数超过100%)。所以基本上我试图让程序循环,如果他们确实输入了无效分数,直到他们输入一个在0到100(含)范围内的分数。
代码:
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)
到目前为止,我的输出看起来像这样:
输入测试分数:-2
得分范围无效,请再次输入:-2
然后停在那里,我需要它继续循环,直到我得到0到100之间的值(包括在内)
答案 0 :(得分:2)
raw_input()
Python 2.x或input()
Python 3.x从用户处获取值。ValueError
进行例外处理。string
到float
执行输入。0
和100
之间。while
循环,即代码来自相应的循环。if
和elif
循环打印课程。代码:
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()
输出:
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
答案 1 :(得分:2)
你在while循环中有详细的测试。然后while循环应用于获得有效分数:
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)
修改强>
让我们移动东西(又名refactoring)
让我们将主程序语句移到一起:
def enter_and_validate_test_score():
score = float(input("Enter a test score: "))
determine_grade(score)
enter_and_validate_test_score()
但determine_grade
函数的名称错误。
程序应首先获得有效分数,然后根据该分数打印分数:
def enter_and_validate_test_score():
score = float(input("Enter a test score: "))
score = validate_score(score)
print_grade(score)
这会让我们看到validate_score
和print_grade
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')
另请注意最终的else:
所以最后整个计划是:
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()