In: Computer Science
4.10 Branching Practice 2 - Python Exercise
#Write the code for the following
#Exercise 1
#Ask the user to input a name using an input statement (no prompt)
and assign it to a variable
#Condition: user input is equal to 'Joseph'
#Action: display the user input on screen
#Exercise 2
#Ask the user to input his/her first name using an input statement
(no prompt) and assign it to a variable
#Condition: user input is equal to 'Joseph'
#Action: display the following phrase (tip: use a concatenator):
The name is: [user entered name]
#Exercise 3
#Ask the user to input his/her first name using an input statement
(no prompt) and assign it to a variable
#Condition: user input is not equal to 'Joseph'
#Action: display the following phrase, The name is: [user entered
name]
#Condition: All other conditions
#Action: display the following phrase as exact text: Expected name
was Joseph!
#Exercise 4
#Ask the user to input a year using an input statement (no prompt)
and assign it to a variable
#Convert the user input to an integer and assign it to a
variable
#Condition: user entry is greater than or equal to 1996
#Action: display the following word on screen: GenZ
#Condition: user entry is greater than or equal to 1979
#Action: display the following word on screen: GenY
#Condition: user entry is greater than or equal to 1964
#Action: display the following word on screen: GenX
#Condition: all other conditions
#Action: display the following phrase on screen: Baby boomer or
older
#Exercise 5
#Use an input statement to ask the user for his/her age (no prompt)
and assign the user entry to a variable
#Convert the user input to an integer and assign it to a
variable
#Write if-elif-else statements with the following conditions and
actions
#Condition: user input less than or equal to 9 Action: display on
screen: Only G is appropriate
#Condition: user input less than or equal to 12 Action: display on
screen: Ok for PG
#Condition: user input less than or equal to 16 Action: display on
screen: Ok for PG-13
#Condition: all other conditions Action: display on screen: Ok for
R
#Regardless of condition, display the following as the last line:
Thanks for checking!
#Exercise 6
#Use an input statement to ask the user for his/her age (no prompt)
and assign the user entry to a variable
#Convert the user input to an integer and assign to a
variable
#Write if-elif-else statements with the following conditions and
actions
#Important: For each Action below, use the same variable name for
the rating in all the if, elif, and else statements
#Condition: user input less than or equal to 9 Action: assign the
letter G as a string to a variable
#Condition: user input less than or equal to 12 Action: assign the
letters PG as a string to a variable
#Condition: user input less than or equal to 16 Action: assign the
phrase PG-13 as a string to a variable
#Condition: all other conditions Action: assign the letter R as a
string to a variable
#Use the + concatenator to display the following on screen
#(replace [rating] with the value of the variable where you stored
the allowed rating):
#You are ok to view [rating] movies!
#Regardless of condition, display the following as the last line:
Thanks for watching movies!
#Exercise 1
#Ask the user to input a name using an input statement (no
prompt) and assign it to a variable
userInput=input()
#Condition: user input is equal to 'Joseph'
if userInput=="Joseph":
#Action: display the user input on screen
print(userInput)
Please check the
compiled program and its output for your reference:
Output:
#Exercise 2
#Ask the user to input his/her first name using an input
statement (no prompt) and assign it to a variable
userInput=input()
#Condition: user input is equal to 'Joseph'
if userInput=="Joseph":
#Action: display the following phrase (tip: use a concatenator):
The name is: [user entered name]
print("The name is: "+userInput)
Please check the
compiled program and its output for your reference:
Output:
#Exercise 3
#Ask the user to input his/her first name using an input
statement (no prompt) and assign it to a variable
userInput=input()
#Condition: user input is not equal to 'Joseph'
if userInput != 'Joseph':
#Action: display the following phrase, The name is: [user entered
name]
print("The name is: "+userInput)
#Condition: All other conditions
else:
#Action: display the following phrase as exact text: Expected name
was Joseph!
print("Expected name was Joseph!")
Please check the
compiled program and its output for your reference:
Output:
#Exercise 4
#Ask the user to input a year using an input statement (no
prompt) and assign it to a variable
userInput=input()
#Convert the user input to an integer and assign it to a
variable
Integer=int(userInput)
#Condition: user entry is greater than or equal to 1996
if(Integer>=1996):
#Action: display the following word on screen: GenZ
print("GenZ")
#Condition: user entry is greater than or equal to 1979
elif(Integer>=1996):
#Action: display the following word on screen: GenY
print("GenY")
#Condition: user entry is greater than or equal to 1964
elif(Integer>=1996):
#Action: display the following word on screen: GenX
print("GenX")
#Condition: all other conditions
else:
#Action: display the following phrase on screen: Baby boomer or
older
print("Baby boomer or older")
Please check the
compiled program and its output for your reference:
Output:
#Exercise 5
#Use an input statement to ask the user for his/her age (no
prompt) and assign the user entry to a variable
userInput=input()
#Convert the user input to an integer and assign it to a
variable
Integer=int(userInput)
#Write if-elif-else statements with the following conditions and
actions
#Condition: user input less than or equal to 9 Action: display on
screen: Only G is appropriate
if Integer<=9:
print("Only G is appropriate")
#Condition: user input less than or equal to 12 Action: display
on screen: Ok for PG
elif Integer<=12:
print("Ok for PG")
#Condition: user input less than or equal to 16 Action: display
on screen: Ok for PG-13
elif Integer<=16:
print("Ok for PG-13")
#Condition: all other conditions Action: display on screen: Ok
for R
else:
print("Ok for R")
#Regardless of condition, display the following as the last line:
Thanks for checking!
print("Thanks for checking!")
Please check the
compiled program and its output for your reference:
Output:
#Exercise 6
#Use an input statement to ask the user for his/her age (no
prompt) and assign the user entry to a variable
userInput=input()
#Convert the user input to an integer and assign to a
variable
Integer=int(userInput)
#Write if-elif-else statements with the following conditions and
actions
#Important: For each Action below, use the same variable name for
the rating in all the if, elif, and else statements
#Condition: user input less than or equal to 9 Action: assign the
letter G as a string to a variable
if Integer<=9:
rating="G"
#Condition: user input less than or equal to 12 Action: assign
the letters PG as a string to a variable
elif Integer<=12:
rating="PG"
#Condition: user input less than or equal to 16 Action: assign
the phrase PG-13 as a string to a variable
elif Integer<=16:
rating="PG-13"
#Condition: all other conditions Action: assign the letter R as
a string to a variable
else:
rating="R"
#Use the + concatenator to display the following on screen
#(replace [rating] with the value of the variable where you stored
the allowed rating):
#You are ok to view [rating] movies!
print("You are ok to view "+rating+" movies!")
#Regardless of condition, display the following as the last
line: Thanks for watching movies!
print("Thanks for watching movies!")
Please check the
compiled program and its output for your reference:
Output:
(Feel free to drop me a comment, If you need any help)
Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...