Question

In: Computer Science

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) & 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!

Solutions

Expert Solution

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


Related Solutions

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...
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,...
In python and comments your code : Exercise 1 1.Write a function waiting for two matrices...
In python and comments your code : Exercise 1 1.Write a function waiting for two matrices and returning their addition, −1 if it is not not possible. 2. Write a function associated with a matrix and a scalar and returning the produced matrix. 3. Write a function accompanying an n × m matrix and a vector n and returning the vector produced by each other, −1 if this is not possible. 4. Write a function accompanying two n × m...
please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
Write code in Python that does the following : An anagram is when the order of...
Write code in Python that does the following : An anagram is when the order of a word can be changed to create a new word (evil, live, and vile for example). Using the dictionary provided, find all of the anagram groups. Which words contain the most anagrams? How large is this group? Here is another example: Alert, alter, and later have 3 in their group. Use the provided dict.txt as your word list, solution must also finish in less...
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
Exercise 1: You are required to re-write the following code to catch the exception that can...
Exercise 1: You are required to re-write the following code to catch the exception that can occur. import java.util.Scanner; public class ExceptionDemo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int number = scanner.nextInt(); // Display the result System.out.println( "The number entered is " + number);           } }
Based on the following Python code, write a Python statement which outputs “December” when the value...
Based on the following Python code, write a Python statement which outputs “December” when the value of the Month is 12 or “November” when the value of the Month is 11 or “Neither” when value of the Month is neither 11 or 12. The user enters the value which is store in the variable namedMonth. Month = int(input("Please enter the value of the month in numerical format such as 11 for November, or 12 for December: " )) sing Python
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT