我正在完成一項作業,我們必須列出您計劃在 Python 中學習的課程。您可以在串列中擁有的最大類數為 5。當輸入第六個類時,它應該要求您將另一個類放入串列中以添加下一個條目。到目前為止,我有我的代碼。當它運行時,它是一個無限回圈,而不是在它達到 6 時停止并顯示訊息。我能做些什么來解決這個問題?
courseList = []
#sets beginning of loop to 0
course = 0
courses = "none"
#input to enter course name
while (courses != "Exit"):
courses = str(input("What is the name of the course? "))
courseList.append(courses)
course = course 1
if (courses == "Exit"):
course = course - 1
#input to drop course
while (course >= 6):
print(courseList)
x = int(input("What course # will you drop? "))
if(1<= x <=6):
courseList.pop(x-1)
course = course -1
print(courseList)
#error message
else:
print("Please select a # between 1-6. ")
uj5u.com熱心網友回復:
我已將兩個while回圈合并在一起并添加了一些if else
試試這個:
CourseList = []
while True:
# Input to enter course name.
course = input("What is the name of the course?\n")
# Check if user wants to exit.
if course == "Exit":
break
# Check the length of the list. If greater than 5, the user selects soemthing to drop.
elif len(CourseList) >= 5:
CourseList.append(course)
# Show user the courses in the list
print(CourseList)
x = input("What course # will you drop?\n")
# Check if x is a number or in range, if not, let the user input again.
while x.isdigit() == False or x < '1' or x > '6':
print("Please choose a number between 1 and 6")
x = input("What course # will you drop?\n")
x = int(x)
CourseList.pop(x - 1)
print(CourseList)
# If none of above, append the course
else:
CourseList.append(course)
uj5u.com熱心網友回復:
即使添加 6 個以上的值,回圈也不會停止的原因是,第一個“while”回圈的條件要求它僅在您輸入"exit".
這實際上意味著您不會因為任何其他原因使第一個 while 回圈結束。
要解決此問題,您應該考慮您要嘗試做什么,我們的初始回應是"if the user tries to add more than 5 courses, he should be prompted to remove an existing course"。很明顯,這里的 if 條件是充分的。
但后來我們意識到,如果用戶試圖洗掉不存在的課程(輸入不在 1 和 6 之間)會怎樣。那么我們想"while the 6th course is being added, keep prompting for removing a course, until successfully removed"。(這個問題可以用干凈的方式處理,這樣我們直觀的第一反應就出現在代碼中,讀到最后)
閱讀了上述推理;
考慮以下:
courseList = []
#sets beginning of loop to 0
course = 0
courses = "none"
#input to enter course name
while (courses != "Exit"):
courses = str(input("What is the name of the course? "))
# Move this to the top, since if user is trying to exit the loop, nothing else needs to be done but that
if (courses == "Exit"):
break
### Add this ###
while course >= 5: # This is essentially also implying if course >= 5 if you think about it
print(courseList)
x = int(input("What course # will you drop? "))
if 1 <= x <= 5:
courseList.pop(x-1)
course = course - 1
else:
print("Please select a # between 1-5. ")
### END ###
courseList.append(courses)
course = course 1
現在,我還想補充一點,第一個中的第二個 while 回圈可能會因為不干凈而脫落。出于這個原因,您可能希望將其移動到一個函式中。
def removeCourse(lst, course):
while course >= 5:
print(courseList)
x = int(input("What course # will you drop? "))
if 1 <= x <= 5:
courseList.pop(x-1)
course = course - 1
else:
print("Please select a # between 1-5. ")
然后使代碼變為:
while (courses != "Exit"):
courses = str(input("What is the name of the course? "))
if (courses == "Exit"):
break
if course >= 5: # Note how we change this to the if condition now, which was our initial response to the problem, because the function handles the case where the input is not valid
removeCourse(courseList, course)
courseList.append(courses)
course = course 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/336404.html
