这是我的文字:
def judgeScoreInput():
if int(input()) in range (11) == [0,1,2,3,4,5,6,7,8,9,10]:
A = int(input("Enter Judge A Score: "))
B = int(input("Enter Judge B Score: "))
C = int(input("Enter Judge C Score: "))
D = int(input("Enter Judge D Score: "))
E = int(input("Enter Judge E Score: "))
judgeScoreInput()
我希望Judge得分的范围限制为10,因此用户输入的数量不能超过10,如果他们这样做会发送错误信息,我该怎么办呢?
答案 0 :(得分:0)
这是铺设事物的一种方式。请注意,我尽可能避免代码重复,并在输入无效时利用递归“重新询问”
def get_score(name):
score = int(input('Enter score for Judge {}: '.format(name)))
if score not in range(11): # Note that we can't assign input to a variable and check that variable at the same time
print('Please enter an integer score 0-10')
return get_score(name)
return score
def judge_score_input():
judges = {}
for judge in ('A', 'B', 'C', 'D', 'E'):
judges[judge] = get_score(judge)
return judges
judge_score_input
将返回将字母A
- E
映射到分数的字典。在原始实现中,局部变量将在函数调用结束时被丢弃。