I need help writing this Python program: Course codes in a community consist of
ID: 3690441 • Letter: I
Question
I need help writing this Python program:
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 wether the course code is valid. If the course code does not follow the rules described above, display an error message. Otherwise, seperate subject from course number and display them seperately.
The following are example outputs:
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
Explanation / Answer
def validate(inp) :
l = len(inp)
if l != 6 :
print "You have entered an invalid course code"
return
part1 = inp[0:3]
part2 = inp[3:]
if not part1.isalpha() :
print "You have entered an invalid course code"
return
try:
number = int(part2)
except ValueError:
print "You have entered an invalid course code."
return
print
print "Subject:",part1
print "Course number:",part2
inp = raw_input("Enter a course code : ")
inp = inp.strip()
validate(inp)