Question

In: Computer Science

Now we want to display which error was thrown in our voting program. Add the appropriate...

Now we want to display which error was thrown in our voting program.

Add the appropriate code to the try/except statement so the exception is displayed.
Run the program and, when prompted, enter the word 'old' so your output matches the output under Desired Output.

# Set the variable
age = input("What is your age?")

# Insert a try/except statement
# here when you convert the input
# to a number using int()
try:
age = int(age)
if age >= 18:
print("Go vote!")

except:
print("Please enter a valid age!")

Output :

invalid literal for int() with base 10: 'old'
Please enter a valid age!

We're still working with pie. Now we want to use the finally clause to display a message no matter what happens with our code.

Add the appropriate code to the try/except statement so that the message "Enjoy your pie!" is displayed regardless of the error caught (or not caught).
Run the program and, when prompted, enter the number 3 so your output matches the output under Desired Output.

# Get Input
pieces = input("How many pieces of pie do you want? ")

# Attempt to convert to integer
try:
percentage = 1/int(pieces)
except ValueError:
print("You need to enter a number!")
except:
print("Something went wrong!")
else:
print("You get", format(percentage, ".2%"), "of the pie!")

Output:

Enjoy your pie!
You get 33.33% of the pie!

both problems are python

Solutions

Expert Solution

Voting Program

# Set the variable
age = input("What is your age?")

# Insert a try/except statement
# here when you convert the input
# to a number using int()
try:
age = int(age)
if age >= 18:
print("Go vote!")

except:
print("invalid literal for int() with base 10:'"+age +"'") // This statement is added
print("Please enter a valid age!")

Output :

What is your age?old
invalid literal for int() with base 10:'old'
Please enter a valid age!

Explanation :

We have added a print() function which displays the required output format as " invalid literal for int() with base 10:'old' ". The format is  "invalid literal for int() with base 10:'"+age +"'".

Working with Pie

# Get Input
pieces = input("How many pieces of pie do you want? ")

# Attempt to convert to integer
try:
print("Enjoy your pie")   // Instead of using finally here the statement is added.
percentage = 1/int(pieces)
except ValueError:
print("You need to enter a number!")
except:
print("Something went wrong!")
else:
print("You get", format(percentage, ".2%"), "of the pie!")

#finally: // This is written only for explanation why it is not used here
# print("Enjoy your pie!") // This is only coment

Output :

Enjoy your pie!
You get 33.33% of the pie!

Explanation :

  • Every statement in the finally block will be executed after checking try blocks. If there is an error, then except block will be executed first and then finally block.
  • else block is executed when there are no errors in the try block otherwise except block is executed.
  • If I add print("Enjoy your pie!") statement to finally block, first either try block will execute one by one by checking each line of code or except block if any of the lines shows error.
  • So, I have added print("Enjoy your pie!") to try block. Here first statement i.e. print("Enjoy your pie!") is executed which is in the right format and does not produce error so it will be executed always whether the error is found or not as the try block checks for error line by line in its block during execution.
  • Then percentage = 1/int(pieces) is checked for error during execution which produces an error if the pieces variable is not an integer value format. So, it will execute if and only if the pieces variable is in integer format otherwise will lead to the execution of except blocks.
  • That is why, I have not used finally block here because desired output format cannot be obtained using finally block.

Related Solutions

Now that we are discussing OLAP and dimensions, the question is: Why do we want our...
Now that we are discussing OLAP and dimensions, the question is: Why do we want our data broken down? What are the benefits?
What is the most appropriate monetary policy for our economy right now.
What is the most appropriate monetary policy for our economy right now.
Write a python program to display a menu with the following options: (1) add, (2) subtract,...
Write a python program to display a menu with the following options: (1) add, (2) subtract, (3) multiply, (4) divide (5) mod, and (6) do nothing. I f the user enters something else (except 1-6), the program should display an error message. Otherwise, it should ask the user for two numbers, perform the calculation, and display the result.
What we want the program to do: We need to write a program, in Java, that...
What we want the program to do: We need to write a program, in Java, that will let the pilot issue commands to the aircraft to get it down safely on the flight deck. The program starts by prompting (asking) the user to enter the (start) approach speed in knots. A knot is a nautical mile and is the unit used in the navy and by the navy pilots. After the user enters the approach speed, the user is then...
We will simulate a dice game in which 2 dice are thrown. If the roll is...
We will simulate a dice game in which 2 dice are thrown. If the roll is 7 or 11, you win. If the roll is 2, 3, or 12, you lose If the roll is any other value, it establishes a point. If with a point established, that point is rolled again before a 7, you win. If, with a point established, a 7 is rolled before the point is rolled again you lose. Build your algorithm incrementally. First write...
What would be appropriate fiscal ( expansionary or contractionary ) policy for our economy right now?...
What would be appropriate fiscal ( expansionary or contractionary ) policy for our economy right now? and why?
I want the program to use 1- D array and display a table as output. Write...
I want the program to use 1- D array and display a table as output. Write a script to simulate the rolling of two dice. The script should use Math.random to roll the first die and again to roll the second die. The sum of the two values should then be calculated. [Note: Since each die can show an integer value from 1 to 6, the sum of the values will vary from 2 to 12, with 7 being the...
We want to save $120,000 now and $150,000 one year from now for possible replacement of...
We want to save $120,000 now and $150,000 one year from now for possible replacement of the old computer. If the replacement will occur at the end of 5 years from now. How much will we have in the account, given the interest rate is 10% per year? Your answer should include the cash flow diagram, the related factors, such as (P/F,i%,n), and the final answer. The calculation should be done manually and also by EXCEL. You may insert the...
We need to create basic program that will simply display a menu and allow the user...
We need to create basic program that will simply display a menu and allow the user to select one of the choices. The menu should be displayed such that each option will be numbered, as well as have one capital letter so as to indicate that either the number or the designated letter can be entered to make their choice. Once the choice is made, the sub-menu for that selection should be displayed. Colors with an odd number of letters...
Where could we cut or add finances for our national healthcare spending, and why?
Where could we cut or add finances for our national healthcare spending, and why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT