Skip to content

Course home

Sign up

Develop code

EasyMediumHard
123456789101112131415
Question 15

A program is being developed to generate a unique staff registration code.

The letter part of the registration code is made up of the first letter of the department joined with the first three letters of the employee's surname. All letters must be in uppercase.

The number part of the registration code is the sum of the ASCII values for each digit in the 6-digit staff code.

The registration code for an employee in Sales with surname Taylor and staff code 840131 is STAY305, all in uppercase.

Original Python Code:

# Global variables
department = ""
surname = ""
staff_code = ""

def make_reg_code(department, surname, staff_code):
    letter_part = department[0] + surname[0:3]
    total = 0
    for char in staff_code:
        total = total + int(char)
    return letter_part + str(total)

# Main program
department = input("Enter department: ")
surname = input("Enter surname: ")
staff_code = input("Enter 6-digit staff code: ")

reg_code = make_reg_code(department, surname, staff_code)
print("Registration Code:", reg_code)

Task:

Amend the code to:

  • Ensure local and global variables with the same names are not confused:
    • Change the names of the local variables inside the make_reg_code function to distinguish them from the global variables with the same names.
  • Welcome the user:
    • Add a procedure called welcome_message, with no parameters, to display a message welcoming the user to the registration system.
    • Call the welcome_message procedure before asking for any user input.
  • Ensure the department and surname are all uppercase:
    • Convert the department name and surname to uppercase after they are entered by the user.
  • Validate the staff code in the main program using code or built-in string methods:
    • Check that only character digits '0' to '9' appear in the staff code.
    • Call the make_reg_code() function only if the staff code is valid.
    • Print a message to tell the user if the staff code is invalid. Invalid input must not be processed.
  • Generate the correct number part of the registration code in the function:
    • Correct the logic error caused by using the int() function in the number part calculation rather than using a function that returns the ASCII value of the character.

Do not add any additional functionality.

FINAL ANSWER:

# Global variables
department = ""
surname = ""
staff_code = ""

def welcome_message():
    print("Welcome to the Registration System")

def make_reg_code(p_dept, p_surname, p_code):
    letter_part = p_dept[0] + p_surname[0:3]
    total = 0
    for char in p_code:
        total = total + ord(char)
    return letter_part + str(total)

# Main program
welcome_message()

department = input("Enter department: ").upper()
surname = input("Enter surname: ").upper()
staff_code = input("Enter 6-digit staff code: ")

if staff_code.isdigit():
    reg_code = make_reg_code(department, surname, staff_code)
    print("Registration Code:", reg_code)
else:
    print("Invalid staff code. Only digits are allowed.")
[15]
Markscheme

Develop code Questions

  1. GCSE
  2. /Computer Science
  3. /Develop code

51 exam-style questions on Edexcel GCSE Computer Science Develop code, covering Decomposition and abstraction to solve problems, Read, write, analyse and refine programs, Converting algorithms into programs, Techniques for readable, maintainable code, Identifying and correcting program errors, and Evaluating program fitness and efficiency. Each one has a worked solution and a mark scheme showing where the marks go.

Question bank