Question

In: Computer Science

4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask...

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!

Solutions

Expert Solution

#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...


Related Solutions

4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 #...
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)...
1. Write Python code to - (a) ask user to enter a list of animals, one...
1. Write Python code to - (a) ask user to enter a list of animals, one item at a time, until “done” to terminate input (b) randomly display a selected item from the list, starting with “I like ______”. When run, the output should look something like this: Enter an item; 'done' when finished: cats Enter an item; 'done' when finished: dogs Enter an item; 'done' when finished: guinea pigs Enter an item; 'done' when finished: done You are done...
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
Python Practice Sample:   Write code to replace every occurrence of THE or the with ### and...
Python Practice Sample:   Write code to replace every occurrence of THE or the with ### and every word ending with the letter s to end with a $. Print the resulting text four words per line (and any remaining words from each paragraph on the last one of each paragraph) "The modern business world goes way beyond the balance sheet. Whether your passion is finance or fashion, economics or the environment, you need an education built for business. At Bentley,...
Using Python write a program that does the following in order: 1. Ask user to enter...
Using Python write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out...
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
Use python programming to write this code and provide a screen short for the code. 2....
Use python programming to write this code and provide a screen short for the code. 2. Write a function that takes one argument (a string) and returns a string consisting of the single character from that string with the largest value. Your function should contain a for loop. You can assume that the input to your function will always be a valid string consisting of at least one character. You can assume that the string will consist only of lower-case...
Write PYTHON CODE to answer the following question: Consider the following data: x = [0, 2,...
Write PYTHON CODE to answer the following question: Consider the following data: x = [0, 2, 4, 6, 9, 11, 12, 15, 17, 19] y = [5, 6, 7, 6, 9, 8, 8, 10, 12, 12] Using Python, use least-squares regression to fit a straight line to the given data. Along with the slope and intercept, compute the standard error of the estimate and the correlation coefficient. Best fit equation y = ___ + ___ x Standard error, Sy/x =...
Write a C++ code to perform the following steps: 1. Ask the user to enter two...
Write a C++ code to perform the following steps: 1. Ask the user to enter two whole numbers number1 and number2 ( Number 1 should be less than Number 2) 2. calculate and print the sum of all even numbers between Number1 and Number2 3. Find and print all even numbers between Number1 and Number2 4. Find and print all odd numbers between Number1 and Number2 5. Calculate and print the numbers and their squares between 2 and 20 (inclusive)...
Write a C++ code to perform the following steps: 1. Ask the user to enter two...
Write a C++ code to perform the following steps: 1. Ask the user to enter two whole numbers number1 and number2 (Number1 should be less than number2) 2. Calculate and print the sum of all even numbers between Number1 and Number2 3. Find and print all even numbers between Number1 and Number2 4. Find and print all odd numbers between Number1 and Number2 5. Calculate and print the numbers and their squares between 1 and 20 (inclusive) 6. Calculate and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT