Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的条件循环不能正常工作?_Python_Loops_Conditional Statements - Fatal编程技术网

Python 为什么我的条件循环不能正常工作?

Python 为什么我的条件循环不能正常工作?,python,loops,conditional-statements,Python,Loops,Conditional Statements,这是制作课程注册程序的一个硬件问题。我觉得我已经完成了大部分工作,但我很难理解为什么在第二个while循环中,如果选择值设置回A,它将不会返回到add course循环 choice = input("Enter A to add course, D to drop course, and E to exit: ") choice = choice.upper() courses = [] while choice == "A": course1 = input("Enter a c

这是制作课程注册程序的一个硬件问题。我觉得我已经完成了大部分工作,但我很难理解为什么在第二个while循环中,如果选择值设置回A,它将不会返回到add course循环

choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()

courses = []

while choice == "A":
    course1 = input("Enter a course to add: ")
    courses.append(course1)
    courses.sort()
    print("Courses Registered: ", courses)
    choice = input("Enter A to add course, D to drop course, and E to exit: ")
    choice= choice.upper()
else:
    while choice == "D":
        drop1 = input("Enter a course to drop: ")
        if drop1 in courses:
            courses.remove(drop1)
            print("Course Dropped")
            courses.sort()
            print("Courses Registered: ", courses)
            choice = input("Enter A to add course, D to drop course, and E to exit: ")
            choice = choice.upper()
        else:
            print("You are not registered in that class")
            choice = input("Enter A to add course, D to drop course, and E to exit: ")
            choice = choice.upper()
    else:
        if choice == "E":
            print("You have exited the program")
        else:
            print("Invalid input")
            choice = input("Enter A to add course, D to drop course, and E to exit: ")
            choice = choice.upper()

直到我放弃了一个类并想继续添加类之后,它才起作用。然后它只在代码中继续,并将打印“无效输入”

使用
break
语句终止
while循环
,否则继续更新选项

courses = []
choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()

while True:
    if choice == "A":
        course1 = input("Enter a course to add: ")
        courses.append(course1)
        courses.sort()
        print("Courses Registered: ", courses)
        choice = input("Enter A to add course, D to drop course, and E to exit: ")
        choice= choice.upper()
    elif choice == "D":
        drop1 = input("Enter a course to drop: ")
        if drop1 in courses:
            courses.remove(drop1)
            print("Course Dropped")
            courses.sort()
            print("Courses Registered: ", courses)
            choice = input("Enter A to add course, D to drop course, and E to exit: ")
            choice = choice.upper()
        else:
            print("You are not registered in that class")
            choice = input("Enter A to add course, D to drop course, and E to exit: ")
            choice = choice.upper()
    elif choice == "E":
        print("You have exited the program")
        break
    else:
        print("Invalid input")
        choice = input("Enter A to add course, D to drop course, and E to exit: ")
        choice = choice.upper()

我建议不要使用while/else结构。您的代码在任何无效输入后退出,而不是重复。第二个循环只接受D,不接受A。因此,只使用一个循环

尝试以下结构

courses = []

while True:
    choice = input("Enter A to add course, D to drop course, and E to exit: ").upper()
    if choice == "E":
        break
    elif choice == "A":
        course1 = input("Enter a course to add: ")
        courses.append(course1)
        courses.sort()
        print("Courses Registered: ", courses)
     elif choice == "D":
         print("drop")
     else:
         print("invalid") 

如果输入不是
E/E
,则希望继续执行,因此应该这样编写

choice = input("Enter A to add course, D to drop course, and E to exit: ")
choice = choice.upper()

courses = []

while choice != "E":
  if choice == "A":
    course1 = input("Enter a course to add: ")
    courses.append(course1)
    courses.sort()
    print("Courses Registered: ", courses)
  elif choice == "D":
    drop1 = input("Enter a course to drop: ")
    if drop1 in courses:
      courses.remove(drop1)
      print("Course Dropped")
      courses.sort()
      print("Courses Registered: ", courses)
    else:
        print("You are not registered in that class")
  else:
    print("Invalid input")

  choice = input("Enter A to add course, D to drop course, and E to exit: ")
  choice = choice.upper()
else:
  print("You have exited the program")