我的病情有问题。我希望变量tabPoint
介于10和100之间。
这是我的代码:
def demand(nb):
tabName = [];
tabPoint = [];
for i in range(nb):
tabName.append(raw_input("Name of the jumper " + str(i+1) + " : "))
tabPoint.append(input("1st jump " + tabName [i] + " The number must be between 10 and 100: " ));
if int (tabPoint[i] < 5 ) and int (tabPoint[i] > 100):
tabPoint.append(input("The number must be between 10 and 100 " ));
return tabName, tabPoint;
name, point = demand(3)
print(name, point)
答案 0 :(得分:0)
你的括号错了。你想要的是tabPoint[i]
,不是 tabPoint[i] < 5
。
所以正确的形式是
if int(tabPoint[i]) > 5 and int(tabPoint[i]) < 100:
tabPoint.append(input("The number must be between 10 and 100 " ))
您还可以使用完成相同操作的简短版本:
if 5 < int(tabPoint[i]) < 100:
tabPoint.append(input("The number must be between 10 and 100 "))
答案 1 :(得分:0)
试试这个:
def demand(nb):
tabName = []
tabPoint = []
for i in range(nb):
tabName.append(input("Name of the jumper "+str(i+1)+": "))
tabPoint.append(0)
# Until a valid entry is made, this prompt will occur
while tabPoint[i] < 10 or tabPoint[i] > 100:
tabPoint[i] = (int(
input("1st jump "+tabName[i]+" The number must be between 10 "
"and 100: ")))
return dict(zip(tabName, tabPoint)) # Returning a dictionary mapping name to point
假设您想打印每个名称并在此之后指出,您可以实现类似:
info = demand(3)
for name, point in info.items():
print(f"Name: {name} Point: {point}")