Professional Documents
Culture Documents
Practical 2: in This Practical, You Will
Practical 2: in This Practical, You Will
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.
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
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.
Figure 2-2: Messages displayed when first or second number user enters is bigger
Figure 2-3: Message displayed when numbers are the same
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
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.
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
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
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.
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.
Ask the user for a number. If the input is numeric, check if the number is even or odd and print an appropriate
message.
Hint: Use the % modulus operator which gives the remainder when a number x is divided by y.
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-2: Sample output from choices 1 and 2 Figure 9-3: Sample output from choices 3 and 4
-- End –