In: Computer Science
4.9 Branching Practice - Python Exercise
#Write the code for the following
# Exercise 1
# Ask user for a number using an input statement (no prompt) &
assign the input to a variable
# Convert the user entry to an integer and assign it to a
variable
# Condition: user entry is greater than zero
# Action: display the following word on screen: positive
# Exercise 2
# Ask user for a number using an input statement (no prompt) &
assign the input to a variable
# Convert user entry to an integer and assign it to a
variable
# Condition: user entry is greater than 99
# Action: display the following word on screen: hundreds
# Exercise 3
# Ask user for a number using an input statement (no prompt) &
assign the input to a variable
# Convert user entry to an integer and assign it to a
variable
# Condition: user entry is less than zero
# Action: display the following word on screen: negative
# Exercise 4
# Ask user for a number using an input statement (no prompt) &
assign the input to a variable
# Convert the user entry to an integer and assign it to a
variable
# Condition: user entry is less than zero
# Action: display the following word on screen: negative
# All other conditions
# Action: display the following word on screen: positive
# Exercise 5
# Ask user for a number using an input statement (no prompt) &
assign the input to a variable
# Convert user entry to an integer and assign it to a
variable
# Condition: user entry is greater than 99
# Action: display the following word on screen: hundreds
# All other conditions
# Action: display the following words on screen: low number
# Exercise 6
# Ask user for a number using an input statement (no prompt) &
assign the input to a variable
# Convert user entry to an integer and assign it to a
variable
# Condition: user entry is equal to 2001
# Action: display the following words on screen: Twin Towers
# Condition: user entry is equal to 2008
# Action: display the following words on screen: Great
Recession
# Condition: user entry is equal to 2017
# Action: display the following words on screen: Hurricane
Maria
# All other conditions
# Action: display the words No Disasters on screen
# Exercise 7
# Ask user for a number using an input statement (no prompt) &
assign the input to a variable
# Convert user entry to an integer and assign it to a
variable
# Condition: user entry is equal to 2001
# Action: display the following phrase on screen using a
combination of the user entry and exact text: 2001 : Twin
Towers
# Condition: user entry is equal to 2008
# Action: display the following phrase on screen using a
combination of the user entry and exact text: 2008 : Great
Recession
# Condition: user entry is equal to 2017
# Action: display the following phrase on screen using a
combination of the user entry and exact text: 2017 : Hurricane
Maria
# All other conditions
# Action: display the following phrase on screen using exact text
only: No disasters!
ANSWERES:-
EXERCISE 1:-
#exercise 1
num=input('Enter a number :')
#converting entry to integer
convertedNum=int(num)
if(convertedNum>0):
print("Positive")
SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-
EXERCISE 2:-
#exercise 2
num=input('Enter a number :')
#converting entry to integer
convertedNum=int(num)
if(convertedNum>99):
print("hundreds")
SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-
EXERCISE 3:-
#exercise 3
num=input('Enter a number :')
#converting entry to integer
convertedNum=int(num)
if(convertedNum<0):
print("negative")
SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-
EXERCISE 4:-
#exercise 4
num=input('Enter a number :')
#converting entry to integer
convertedNum=int(num)
if(convertedNum<0):
print("negative")
else:
print("positive")
SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-
EXERCISE 5:-
#exercise 5
num=input('Enter a number :')
#converting entry to integer
convertedNum=int(num)
if(convertedNum>99):
print("hundreds")
else:
print("low number")
SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-
EXERCISE 6:-
#exercise 6
num=input('Enter a number :')
#converting entry to integer
convertedNum=int(num)
if(convertedNum==2001):
print("Twin Towers")
elif(convertedNum==2008):
print("Great Recession")
elif(convertedNum==2017):
print("Hurricane Maria")
else:
print("No Disasters")
SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-
EXERCISE 7:-
#exercise 7
num=input('Enter a number :')
#converting entry to integer
convertedNum=int(num)
if(convertedNum==2001):
print(num+" : Twin Towers")
elif(convertedNum==2008):
print(num+" :Great Recession")
elif(convertedNum==2017):
print(num+" :Hurricane Maria")
else:
print("No Disasters")
SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-
HAPPY LEARNING