C++ Programming: From Problem Analysis to Program Design
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN: 9781337102087
Author: D. S. Malik
Publisher: Cengage Learning
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question
Python Lab 5: if statements with else and elif
1. Write an if statement to
Ask the user to enter a number. Determine if the number is divisible by 5.
if (number % 5 == 0):
print (str(number) + ' is divisible by 5')
determine if a number is
divisible by 5.
2. Write an if/else statement.
An else statement can follow an if statement. The code in an else
statement is executed if the expression in the if statement is false.
if (number % 5 == 0):
print (str(number) +
' is divisible by 5')
else:
print (str(number) + ' is divisible by 5')
Notice that an else statement does not have a Boolean expression.
Write an additional iflelse statement to see if the number is divisible by 2.
Test your code with many different inputs.
3. Write an if/elif/else
The command elif can be used to make a series of if statements more
efficient. As soon as one of the expressions is true, the other ones are
skipped. elif is short for 'else if.
sequence.
Ask the user to enter a State/Province name, and print out the capital of
that state. Your code should handle at least 6 different inputs. Use an
if/eliflelse structure
if (state == 'Wisconsin'):
print ('Madison')
elif (state == 'Colorado)':
print('Denver')
else:
print ('I do not know that one')
Test your code with many different input values.
4. Use elif in a function
At a certain public pool, entrance prices are as follows:
Under 2 years: free, Age 2-11: $3, Age 11-60: $6, Over 60: $4
Complete a function that takes the age in years as input and returns the
price in dollars (without the dollar sign).
definition.
def pool_admission (age):
if (age < 2):
return e
elif (age < 12):
return 3
Notice that elif reduces possibilities and makes the code easier to read.
expand button
Transcribed Image Text:Python Lab 5: if statements with else and elif 1. Write an if statement to Ask the user to enter a number. Determine if the number is divisible by 5. if (number % 5 == 0): print (str(number) + ' is divisible by 5') determine if a number is divisible by 5. 2. Write an if/else statement. An else statement can follow an if statement. The code in an else statement is executed if the expression in the if statement is false. if (number % 5 == 0): print (str(number) + ' is divisible by 5') else: print (str(number) + ' is divisible by 5') Notice that an else statement does not have a Boolean expression. Write an additional iflelse statement to see if the number is divisible by 2. Test your code with many different inputs. 3. Write an if/elif/else The command elif can be used to make a series of if statements more efficient. As soon as one of the expressions is true, the other ones are skipped. elif is short for 'else if. sequence. Ask the user to enter a State/Province name, and print out the capital of that state. Your code should handle at least 6 different inputs. Use an if/eliflelse structure if (state == 'Wisconsin'): print ('Madison') elif (state == 'Colorado)': print('Denver') else: print ('I do not know that one') Test your code with many different input values. 4. Use elif in a function At a certain public pool, entrance prices are as follows: Under 2 years: free, Age 2-11: $3, Age 11-60: $6, Over 60: $4 Complete a function that takes the age in years as input and returns the price in dollars (without the dollar sign). definition. def pool_admission (age): if (age < 2): return e elif (age < 12): return 3 Notice that elif reduces possibilities and makes the code easier to read.
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,