Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Practical 2

In this practical, you will …


 Practice using if-else to control the flow of your Python programs
 Practice using loop statements to check user’s inputs
 Implement nested statements
 Implement numeric checks on user’s inputs
 Use functions for longer programs
 Implement try-except
Practice – Conditional

1. Determine if a number is odd or even

Ask the user for a number. If the input is not numeric, display an error message and exit the program. Hint: use the
isnumeric() string function.
If the input is numeric, check if the number is even or odd and print out an appropriate message to the user. Hint:
Use the % modulus operator which gives the remainder when a number x is divided by another number y.

Your program should display output like these.

Figure 1-1: Error message when user enters a non-numeric input

Figure 1-2: Message displayed when user enters a Figure 1-3: Message displayed when user enters a valid
valid number that is even number that is odd

2. Determine which number is bigger

Ask the user for two numbers. If the inputs are not numeric, display an error message and exit the program.
If the inputs are numeric, check which number is bigger and print an appropriate message to the user. Do also check
if the numbers are equal.

Your program should display output like these.

Figure 2-1: Error message when user enters a non-numeric input

Figure 2-2: Messages displayed when first or second number user enters is bigger
Figure 2-3: Message displayed when numbers are the same

3. Using nested if-else

Company X revises the increment to the salary of a staff based on the following criteria. For example, if a staff has
worked for less than 10 years and is earning a salary of $1999, he would get an increment of $200.
years salary increment

Less than 10 Less than $1000 $100

Less than $2000 $200

$2000 or more $300

10 or more Less than $1000 $200

Less than $2000 $300

$2000 or more $400

Write a Python program that will prompt for 2 numbers which represent years of service and salary of a staff. The
program will then display the increment of the staff. As before, do the necessary checks for numeric inputs.

Figure 3-1: Sample output from program


Practice – Iterations

4. Prompt user for a starting number and an ending number. Display all the odd numbers between starting number and
ending number exclusive, separated by tab (“\t”).

Saying “between 1 and 10” is somewhat ambiguous; usually people will say “between 1 and 10
inclusive” or “between 1 and 10 exclusive” to clarify when there is no other context. Both “between…
and…” and “from…to…” are usually considered inclusive unless otherwise specified.

Ref: https://english.stackexchange.com/questions/7871/between-a-and-b-or-from-a-to-b
Hint: you should use the step parameter to do this.

If the first number is bigger than the second number, display the output in reverse. Sample outputs are shown
below.

Figure 4-1: Sample output from program if first number is smaller than second number

Figure 4-2: Sample output from program if first number is bigger than second number

5. For loops for user’s inputs

Prompt user for a starting number and an ending number. Display all the even numbers between starting number
and ending number exclusive using a for loop, separated by tab (“\t”).
Saying “between 1 and 10” is somewhat ambiguous; usually people will say “between 1 and 10
inclusive” or “between 1 and 10 exclusive” to clarify when there is no other context. Both
“between…and…” and “from…to…” are usually considered inclusive unless otherwise specified.

Ref: https://english.stackexchange.com/questions/7871/between-a-and-b-or-from-a-to-b

Figure 5-1: Sample output from program


6. While loops for user’s inputs

Prompt user repeatedly to enter a number, or “Q” to quit. Sum each number entered by the user until “Q” is
entered, after which you are to display the total of all the numbers entered. Sample runs are shown below.

Figure 6-2: Sample output from program

Figure 6-1: Sample output from program


Practice – Exceptions

7. We cannot assume that a user input will be correct. For example, a user may enter a non-numeric that resulted in
run-time error as shown below.

Write a script to prompt user to enter an integer repeatedly until a numeric value is entered as shown below.

You are required to use try-except in your script.

8. Determine if a number is odd or even.

Ask the user for a number. If the input is numeric, check if the number is even or odd and print an appropriate
message.

You are required to use try-except in your script.

Hint: Use the % modulus operator which gives the remainder when a number x is divided by y.

Your program should display output like these.

Figure 8-1: Error message when user enters a non-numeric input

Figure 8-2: Message displayed when user enters a valid number that is even

Figure 8-3: Message displayed when user enters a valid number that is odd
Practice – Functions

9. Define a Python script to process user input based on the menu shown below. You are required to call a function to
display the main menu and also for each user option.

Figure 9-1: Sample output from main function

Figure 9-2: Sample output from choices 1 and 2 Figure 9-3: Sample output from choices 3 and 4

-- End –

You might also like