New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only. T&Cs apply

# If statements

Learn algorithms, logic, and Python basics. Create simple programs, grasp computer science fundamentals, and see its impact across various fields.

In this activity, you will practice your skill of writing if statements in Python.

Imagine that a teacher in a high school asked you to create a computer program that will help her to classify student marks. Your program will read a mark (integer) from the user and will print the classification of the mark according to the following rules:

The program will print:

1. ‘A’ if the mark is between 8…10 (inclusive).
2. ‘B’ if the mark is between 5…7 (inclusive).
3. ‘C’ if the mark is between 4…6 (inclusive).
4. ‘F’ if the mark is between 1…3 (inclusive).
5. ‘Wrong input’ if anything else is provided.

### The problem you need to solve

You need to write a Python program that will read an input from the user, representing a mark, and it will print the classification according to the rules above.

### Prepare

The files you will need to complete this exercise, and the solution to it, can be found in the Supporting files and solutions .zip file. Use the files in the folder ‘lab 4’.

Open Visual Studio Code and then open the if_statements.py file. You will see a skeleton code with multiple TODO comments to help you complete this exercise. Each TODO block you will see (they appear as Python comments with leading # and the keyword TODO) is a small sub-task for you.

### Complete the code

Have a look at all the TODO comments and complete the code required for each.
Once you complete the code for each TODO, try to run the solution. Open a terminal: from the menu bar, Terminal → New Terminal. Then run python if_statements.py

Example Output:

❯ python if_statements.pyPlease provide the mark of the student: 10Student grade: A❯ python if_statements.pyPlease provide the mark of the student: 5Student grade: B❯ python if_statements.pyPlease provide the mark of the student: 4Student grade: C❯ python if_statements.pyPlease provide the mark of the student: 3Student grade: F❯ python if_statements.pyPlease provide the mark of the student: -1Wrong input: value must be between 1 and 10

### Solutions

Remember, there is usually more than one solution. If you thought of something else, it’s not necessarily wrong. If your solution works, that’s great. Read my code regardless and see if there is something I do differently and think of why that might be, or judge if it’s better.

A good programmer is one that reads others’ code!