In PYTHON Course codes in a community consist of two parts: subject and course n
ID: 3691452 • Letter: I
Question
In PYTHON
Course codes in a community consist of two parts: subject and course number. Subject must consist of 3 letters while course number must consist of 3 digits. For example, in the course code 'CIS115, subject is 'CIS', and course number is '115'. Write a program to do the following:
(a) Ask the user to enter a course code.
(b) Strip all the leading and trailing whitespace characters from the code. Display the stripped code.
(c) Test whether the course code is valid. If the course code does not follow the rules described above, display an error message. Otherwise, separate subject from course number and display them separately.
Example output:
Enter course code: CIS115
Subject: CIS
Course number: 115
Enter course code: CIS1155
You have entered an invalid course code
Enter course code: CS2567
You have entered an invalid course code
Enter course code: CIS25K
You have entered an invalid course code
Explanation / Answer
code = input("Enter course code:")
code = code.replace(" ","")
if(len(code)!=6):
print("You have entered an invalid course code")
exit
sub = code[0:3]
if(not sub.isalpha()):
print("You have entered an invalid course code")
exit
cou = code[3:6]
if(not cou.isdigit()):
print("You have entered an invalid course code")
exit
print("Subject:",sub)
print("Course number:",cou)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.