In: Computer Science
Python
#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!
In this Python program,
(I believe that I made the code simple and understandable. If you still have any query, Feel free to drop me a comment)
Code:
#Use an input statement to ask the user for his/her age and
assign the user entry to a variable
userInput=input()
#Convert the user input to an integer and assign to a
variable
userAge=int(userInput)
#Writing an if-elif-else statements with the following conditions
and actions
#Condition: user input less than or equal to 9 Action: assign
"G" to a variable
if userAge<=9:
rating="G"
#Condition: user input less than or equal to 12 Action: assign "PG"
to a variable
elif userAge<=12:
rating="PG"
#Condition: user input less than or equal to 16 Action: assign
"PG-13" to a variable
elif userAge<=16:
rating="PG-13"
#Condition: all other conditions Action: assign "R" 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):
print("You are ok to view "+rating+" movies!")
#Regardless of condition, display the last line
print("Thanks for watching movies!")
Note: Please check the screenshot for Indentation, This compiler will usually remove all the Indentation which is provided.
Please check the
compiled program and its output for your reference:
Output:
Sample
case-1:
Sample
case-2:
Sample
case-3:
Sample
case-4:
Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...