Question

In: Computer Science

How can i quit this python program. #******************************************************************************** #Program : SearchClassInfo.py #Description: This program will ask...

How can i quit this python program.

#********************************************************************************
#Program : SearchClassInfo.py

#Description: This program will ask a user to enter 6 courses and will allow him
# to search a class by course number, room number, intructor or time,
# the program will run until the user stop it.
#********************************************************************************
from os import sys
def main():
#Definition of 4 lists
courses = []
rooms = []
instructors = []
times = []
#Allow user to enter six courses
for x in range(2):
Course_Number =input('Enter the course number: ')
Room_Number = input('Enter a room number: ')
Instructor =input('Enter the Instructor of the course: ')
Time = input('Enter the time the course meets: ')
#Add elements in appriate list.
courses.append(Course_Number)
rooms.append(Room_Number)
instructors.append(Instructor)
times.append(Time)


loop = 1 #initialisation of a flag.

while loop != 0:
try:
#Display a menu of four choices to make to search a class.
print('\nEnter 1 to search by course number: ')
print('Enter 2 to search by room number: ')
print('Enter 3 to search by instructor: ')
print('enter 4 to search by time: ')

choice = int(input('Enter your choice: '))#ask user to enter his choise for search.

if choice == 1 :
#ask user to enter course number for search of corresponding elements.
Course_Number = input('Enter a course number: ')
if Course_Number in courses: #verify if the couse number entered is in the list
#extract the index of the course number in list.
i=courses.index(Course_Number)
#printing elements on the same index as course number in other lists.
print('\nThe detail for course',courses[i], 'are:'
'\nRoom:', rooms[i],
'\nInstructor:',instructors[i],
'\nTime:',times[i], end=' ''\n')
else:#message to alerte when course number does not exit
print('\nThis course number is not on schedule')
  
elif choice == 2 :
# #ask user to enter room number for search of corresponding elements.
Room_Number = input('Enter a room number: ')
if Room_Number in rooms:#verify if the room number entered is in the list
#extract the index of the room number in list.
i=rooms.index(Room_Number)
#printing elements on the same index as room number in other lists.
print('\nThe detail for course',courses[i], 'are:'
'\nRoom:', rooms[i],
'\nInstructor:',instructors[i],
'\nTime:',times[i], end=' ' '\n')
else:#message to alerte when room number does not exit
print('\nThis room number is not on schedule')

elif choice == 3 :
Instructor = input('Enter the Instructor of the course: ')
if Instructor in instructors:
i=instructors.index(Instructor)
print('\nThe detail for course',courses[i], 'are:'
'\nRoom:', rooms[i],
'\nInstructor:',instructors[i],
'\nTime:',times[i], end=' ' '\n')
else:
print('\nThis instructor is not on schedule')
  
elif choice == 4 :
Time = input('Enter the time the course meets: ')
if Time in times:
i=times.index(Time)
print('\nThe detail for course',courses[i], 'are:'
'\nRoom:', rooms[i],
'\nInstructor:',instructors[i],
'\nTime:',times[i], end=' ' '\n')
else:
print('\nThis time is not on schedule')
  
elif choice!=' ':
print('Choice goes from 1--->4')
loop = 0
  


print('\nDo you want to continue? Enter y for yes')
control = input(' ')

if control == 'y'or'Y':
loop = 1

elif control!=' ':
sys.exit(0)

except:
print('incorrect choice, must be 1,2,3 or4')
pass
  
main()

Solutions

Expert Solution

The code in image:

The code in the text:

from os import sys
def main(): #Definition of 4 lists
   courses = []
   rooms = []
   instructors = []
   times = [] #Allow user to enter six courses
   for x in range(2):
       Course_Number =input('Enter the course number: ')
       Room_Number = input('Enter a room number: ')
       Instructor =input('Enter the Instructor of the course: ')
       Time = input('Enter the time the course meets: ') #Add elements in appriate list.
       courses.append(Course_Number)
       rooms.append(Room_Number)
       instructors.append(Instructor)
       times.append(Time)
   loop = 1 #initialisation of a flag.
   while loop!=0:
       try: #Display a menu of four choices to make to search a class.
           print('\nEnter 1 to search by course number: ')
           print('Enter 2 to search by room number: ')
           print('Enter 3 to search by instructor: ')
           print('enter 4 to search by time: ')
           choice = int(input('Enter your choice: '))#ask user to enter his choise for search.
           if choice == 1 : #ask user to enter course number for search of corresponding elements.
               Course_Number = input('Enter a course number: ')
               if Course_Number in courses: #verify if the course number entered is in the list#extract the index of the course number in list.
                   i=courses.index(Course_Number) #printing elements on the same index as course number in other lists.
                   print('\nThe detail for course',courses[i], 'are:''\nRoom:', rooms[i],'\nInstructor:',instructors[i],'\nTime:',times[i], end=' ''\n')
               else:#message to alerte when course number does not exit
                   print('\nThis course number is not on schedule')
           elif choice == 2 : # #ask user to enter room number for search of corresponding elements.
               Room_Number = input('Enter a room number: ')
               if Room_Number in rooms: #verify if the room number entered is in the list #extract the index of the room number in list.
                   i=rooms.index(Room_Number) #printing elements on the same index as room number in other lists.
                   print('\nThe detail for course',courses[i], 'are:''\nRoom:', rooms[i],'\nInstructor:',instructors[i],'\nTime:',times[i], end=' ' '\n')
               else:#message to alerte when room number does not exit
                   print('\nThis room number is not on schedule')
           elif choice == 3 :
               Instructor = input('Enter the Instructor of the course: ')
               if Instructor in instructors:
                   i=instructors.index(Instructor)
                   print('\nThe detail for course',courses[i], 'are:''\nRoom:', rooms[i],'\nInstructor:',instructors[i],'\nTime:',times[i], end=' ' '\n')
               else:
                   print('\nThis instructor is not on schedule')
           elif choice == 4 :
               Time = input('Enter the time the course meets: ')
               if Time in times:
                   i=times.index(Time)
                   print('\nThe detail for course',courses[i], 'are:''\nRoom:', rooms[i],'\nInstructor:',instructors[i],'\nTime:',times[i], end=' ' '\n')
               else:
                   print('\nThis time is not on schedule')
           elif choice!=' ':
               print('Choice goes from 1--->4')
              
          
           print('\nDo you want to continue? Enter y for yes and n for no')
           control = input(' ')
          
           if control == 'y'or control=='Y':
               loop = 1
           elif control == 'N' or control=='n':
               loop=0
       except:
           print('incorrect choice, must be 1,2,3 or4')
           pass
      
  
main()

Output:

I just changed the condition in elif, so if the user enters n for no then the loop=0 and the while loop will be stopped.

I tried sys.exit() but it didn't worked for me.

If you have any doubt comment me.


Related Solutions

Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
how can i make this program in python? Captain Øks is on his way to conquer...
how can i make this program in python? Captain Øks is on his way to conquer Mosefjell. He has a map of the area and he is in his camp at the designated place on the map. The map is divided into cells. For each cell, the height is given. Captain Øks is not happy, the landscape is full of hills, and he hates to climb both up and down. He has hired you to write a program that will...
You have been asked to create a python program that will ask the user how many...
You have been asked to create a python program that will ask the user how many tickets the user would like to purchase for graduation. The user will then choose the number of tickets needed and the program will list the cost of the tickets, a service fee, tax, and the total of the purchase. Specifications: The program should only accept whole numbers when receiving input for the number of tickets needed. No calculations should be performed if input is...
How to make a python program that imports only turtle and math library where I can...
How to make a python program that imports only turtle and math library where I can click once on the screen to set the center of the square, move the mouse to define the edge-length of the square; click a second time to draw the square with the defined edge-length and center point?
How can I write a Python program that runs without crashing even though it has an...
How can I write a Python program that runs without crashing even though it has an error that a compiler would have caught and how can I write a Java program that crashes even after the compiler checked it. Please add comments that explain the problem, the outcome, and what this proves in both of the programs.
Write a Python program that computes the income tax for an individual. The program should ask...
Write a Python program that computes the income tax for an individual. The program should ask the user to enter the total taxable income for the year. The program then uses the tax bracket (as shown below) to calculate the tax amount. This is based on a progressive income tax system which is how income tax is calculated in the U.S. As a result, this for example means that only income above $500,001 is taxed at 37%. Income of lower...
Write a Python program that computes the income tax for an individual. The program should ask...
Write a Python program that computes the income tax for an individual. The program should ask the user to enter the total taxable income for the year. The program then uses the tax bracket (as shown below) to calculate the tax amount. This is based on a progressive income tax system which is how income tax is calculated in the U.S. As a result, this for example means that only income above $500,001 is taxed at 37%. Income of lower...
Program a math quiz on Python Specifications: - The program should ask the user a question...
Program a math quiz on Python Specifications: - The program should ask the user a question and allow the student to enter the answer. If the answer is correct, a message of congratulations should be displayed. If the answer is incorrect, a message showing the correct answer should be displayed. - the program should use three functions, one function to generate addition questions, one for subtraction questions, and one for multiplication questions. - the program should store the questions and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT