Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem Help!
#1
I'm having trouble with the code for this problem:
Write a program for course registration. Students can use this program to add and drop courses. There should be a loop in the program that tells the user to enter A to add course, D to drop course or E to exit. If A is chosen, ask the user to enter the course to add. If the user is already registered in that course, display the message “You are already registered in that course”; otherwise, add the course to the user’s course list, display the message “Course added”, sort the list and display the list. If the user chooses D, ask the user to enter the course to drop. If the user is not registered in that course, display the message “You are not registered in that course”; otherwise, remove the course from the user’s course list, display the message “Course dropped” and display the list. The loop will stop when the user enters E.

Basically I'm having problems with having three while loops. I know looking at it, the "again" function is only going to make it repeat the first body of the first while statement. I need to find a way to access all three loops or know how to rewrite the code to make it work. Sorry for the long text.
course_list =[]
decision=input('Enter A to add course, D to drop course, and E to exit:[A/D/E]')
decision=decision.upper()

while decision == 'A':
    add_course=input("Enter course to add:")
    add_course.upper()
    if add_course in course_list:
        print("You are already registered in that course")
        print('Courses Registered:', course_list)
    else:
        course_list.append(add_course)
        print('Course added')
        print('Courses Registered:', course_list)
    again=input('Enter A to add course, D to drop course, and E to exit:[A/D/E]')

while decision == 'D':
    del_course=input('Enter course to drop:')
    del_course.upper()
    if del_course in course_list:
        course_list.remove(del_course)
        print('Course Dropped')
        print('Courses Registered:', course_list)
    else:
        print('You are not registered for that course')
        print('Courses Registered:', course_list)
    again=input('Enter A to add course, D to drop course, and E to exit:[A/D/E]')

while decision == 'E':
    print('Press ENTER to EXIT')
Reply
#2
I would use one while loop that starts before the decision input. All the while loops you currently have should be converted to an if/elif chain. The exit command would break out of the one while loop. You could add an else statement to the end to say 'Please choose A, D, or E' or some other warning on invalid input.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Sep-18-2019, 09:55 PM)ichabod801 Wrote: I would use one while loop that starts before the decision input. All the while loops you currently have should be converted to an if/elif chain. The exit command would break out of the one while loop. You could add an else statement to the end to say 'Please choose A, D, or E' or some other warning on invalid input.

Thank you for the quick response! I tried earlier to make one while loop but failed. I did not, however, try to start the while loop before the decision input. I'm honestly confused about how to start the while loop before the decision input. Any suggestions? Thank you so much.
Reply
#4
courses = []
while True:
    decision=input('Enter A to add course, D to drop course, and E to exit:[A/D/E]')
    decision=decision.upper()
    if decision == 'A':
        ...
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Sep-18-2019, 11:52 PM)ichabod801 Wrote:
 courses = [] while True: decision=input('Enter A to add course, D to drop course, and E to exit:[A/D/E]') decision=decision.upper() if decision == 'A': ... 

Thank you so much! I was about to pull what little hair I have left out!
Reply
#6
Don't worry. Eventually you'll be like me and you won't have any hair left to pull out.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020