【发布时间】:2017-08-29 02:06:48
【问题描述】:
我无法让 getClasses 函数中的 while 循环在它应该结束的时候结束。 getNum 和 counter 都设置为整数。我也尝试过 while True 和 if 语句来查看是否有帮助,但这只会给我一个无限循环。我知道这是一个简单的问题,但我无法弄清楚我做错了什么。请帮忙。
def getNum():
while True:
try:
num = int(raw_input("How many classes do you have:\n")) #Asks the user for the number of classes he/she has.
break
except:
print("Input must be a number")
return num
def getGrades(): #Gets the grades from the user
counter2 = 1
global grades
if counter2 <= getNum: #If the 2nd counter is less than or equa to the number of classes...
while True:
try:
grades = int(raw_input("What is the grade for class %s:\n" %counter2)) #Asks the user what their grades are for 'counter' class.
counter2 += 1 #...increase the 2nd counter by 1
break
except:
print("Input must be a number")
return grades
def getClasses(): #Gets the user's classes
counter = 1
getNum()
while counter <= getNum: #If the counter is less than or equal to the number of classes...
classes = str(raw_input("Class %s is what:\n" %counter)) #Asks the user what their 'counter' class is.
subjects[classes] = getGrades()
counter += 1 #...increase the counter by 1
return subjects
【问题讨论】:
-
什么时候结束?什么是 getNum?
-
counter2 <= getNum和getNum()?它不能既是整数又是函数……这段代码可能根本不起作用。 -
什么是getNum???
-
getNum 函数应该返回一个整数。
-
getNum() 函数返回的值是多少? while 循环迭代 getNum() 次。
标签: python while-loop boolean