Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
125 views22 pages

Python Chapter 4.3-4.4

Here are the steps to solve this problem: 1. Request user to enter a number between -10 and 10 and store it in a variable num 2. Check if num is between -10 and 10, if not display "Invalid number" and stop 3. Check if num is positive - If yes, check if it is even and display "Even positive number" or "Odd positive number" - If no, check if it is even and display "Even negative number" or "Odd negative number" 4. Display the final message Let me know if you need help with the code.

Uploaded by

shahida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views22 pages

Python Chapter 4.3-4.4

Here are the steps to solve this problem: 1. Request user to enter a number between -10 and 10 and store it in a variable num 2. Check if num is between -10 and 10, if not display "Invalid number" and stop 3. Check if num is positive - If yes, check if it is even and display "Even positive number" or "Odd positive number" - If no, check if it is even and display "Even negative number" or "Odd negative number" 4. Display the final message Let me know if you need help with the code.

Uploaded by

shahida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

▪ Explain the structure of if..

elif structure and how it works


▪ Write if..elif statement
▪ Evaluate if..elif statement
▪ Practicals
if condition1:
action 1
elif condition2:
action two
elif condition3
action3
else:
action4
actionN
▪ When condition1 test result is True,
action 1 is executed and other
condition will be skipped
▪ When the condition 1 test result is
False, action 1 will be skipped and
condition 2 will be tested
▪ Only True condition will execute its
action.
▪ When no True result is accessed
among the conditions, default action
will be executed, if available
▪ Consider the following problem:
▪ Letter grade awarded to students based on the following score criteria
SCORE LETTER
RANGE GRADE
80-100 A
70-79 B
60-69 C
50-59 D
0-49 F
other invalid

▪ In order to classify the score into either grade A, B, C, D or F we need multiple if


that provide multiple conditions
▪ The score criteria/condition can be written as follows:

SCORE LETTER CONDITIONS


RANGEs GRADE
80-100 A score>=80 and score<=100
70-79 B score>=70 and score<=79
60-69 C score>=60 and score<=69
50-59 D score>=50 and score<=59
49 And below F USE else
score = eval(input(“enter score”))
if score >= 80 and score<=100:
print("Grade A")
elif score >= 70 and score<=79: score >=80 score >= 70 score >= 60 score >= 50 score >= 0
score and and and and and output
print("Grade B") score<=100 score<=79 score<=69 score<=59 score<=49
elif score >= 60 and score<=69: 85 TRUE Grade A
print(“Grade C") 60 FALSE FALSE TRUE Grade C
20 FALSE FALSE FALSE FALSE TRUE Grade F
elif score >= 50 and score<=59: 100 TRUE Grade A
print(“Grade D") 54 FALSE FALSE FALSE TRUE Grade D
150 FALSE FALSE FALSE FALSE FALSE Invalid
elif score >= 0 and score<=49: -90 FALSE FALSE FALSE FALSE FALSE Invalid
print(“Grade F")
else:
print(“Invalid")
vowel=0
ltr=input(“enter a letter”)
if ltr==’A’or ltr==’E’or ltr==’I’or ltr==’O’or ltr==’U’:
Vowel=Vowel+1;
print(“Number of vowels ?”,Vowel)

Assume the input:


i. I
ii. ABC
iii. X
iv. a
x=2;
y=3;
num=eval(input(“Enter a number”));
if num==x or num==y-x:
print(”Buckle my shoe”)
elif num<=4:
print(”Shut the door”)
elif num>=x+y and num<=x*y:
print(”Pick up the sticks”)
elif num==7 or num==8 :
print(”Lay them straight”)
else:
print(“Start all over again”)

Assume input is i) 3 ii) 0


▪ Read in a temperature and print out what sport is appropriate for that temperature
using the following guidelines:

SPORT TEMPERATURE
Swimming > 85
Tennis 70< temp <=85
golf 32 < temp <=70
Skiing 10 < temp <=32
Chinese Checkers <= 10
Accept an age.

If age is less than 6,

set price to 0,

otherwise if age is between 6 and 17,

set price to 3.75,

otherwise if price is 17 and above

set price to 5

Display the price


Accept an input for percentage of cloud cover and display an appropriate
descriptor.

Percentage of Cloud Cover Descriptor


0-30.1 clear
31-70.1 partly cloudy
71-99.1 cloudy
100 overcast
1. Write a program segment that accepts a Book Number. Use if statements to locate
the book number in the library stack and display the location of the book base on
the following Book Number criteria and location

Book Number Location


100 to 199 basement
200 to 500 and over 900 main floor
501 to 900 except 700 to 750 upper floor
700 to 750 archives
Otherwise Not available
▪ Identify the structure for nested if
▪ Identify how nested if works
▪ Evaluate nested if
▪ Simplified nested if
▪ Practicals
▪ A nested if statement is defined as a block of if statement placed inside another if
block or in other word, an if statement that contains another if statement

If condition1:
if condition2:
statement1
else: nested
statement2
else:
statement 3
statementN
x = int(input('Enter your age: '))
if x > =21: X
if x < =60:
print(‘Congratulations!, You are too Eligible!')
else:
print('Welcome, you are of the right age!') FALSE
else: X>=21
print(‘Sorry,You are too young!')
“Sorry, You are
TRUE too young”
X X>=21 X<=60 Output
5 False - Sorry, You are too young FALSE
X<=60
25 True True Congratulations!, You
are Eligible
TRUE “Sorry, You are too
120 True False Sorry, You are too old old”

The condition in nested can also be written as “Congratulations!,


You are Eligible”
follows:

if X>=21 and X<=60:


Err = int(input(Type error number: '))
if Err >0: Err
if Err == 5:
print(‘Restart the machine')
else:
print(‘Refer to attached Manual!’) FALSE
else: Err>0
print(‘Contact Technician: 03-23232323’) “Contact
TRUE Technician: 03-
Err Err>0 Err==5 Output 23232323”

0 False - Contact Technician: 03-23232323 FALSE


Err==5
25 True True ‘Restart the machine'
120 True False ‘Refer to attached Manual!’ TRUE “Refer to attached
Manual”

The condition in nested can also be written as “Restart the


machine”
follows:

if Err>=0 and Err ==5:


What output produced by the following if statement

if (a<b):
if (c<5):
print("Hello")
print("Finish already')

Assume the value for a,b and c:


i) a=3; b=2; c=5;
ii)a=1;b=2; c=-1;
iii)a=1;b=2; c=6;
Simplified the following nested if statement/Rewrite the following nested if statement into a single if
statement.

if (Price<500):
if (TotalPurchased>100):
if (Status==1):
Discount=0.5;
▪ Ask the user to enter a exam score between 0 and 50 inclusively .
▪ Verify that the number between the specified range. Once verified
▪ Calculate the percentage of score
▪ Display “PASS” if the percentage is between 50 and 100 inclusively.
▪ Otherwise, display “FAIL”

▪ Otherwise, display “Invalid Score”


▪ Request user to enter a number between -10 and 10.
▪ Display a message “The number entered is “, followed by the number.
▪ If the number is positive, test if the number is even. Then, display a message “Even
positive number”. Otherwise, display “Odd positive number”
▪ Otherwise, test if the number is even. Then display a message “Even negative
number”. Otherwise, display “Odd negative numbers”

You might also like