Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives: The objective of this homework assignment is to demonstrate your und

ID: 3867554 • Letter: O

Question

Objectives:

The objective of this homework assignment is to demonstrate your understanding of exception handling and try/except.

Instructions:

For try/except, there are two places in which you will receive user input. The first is in a function that displays a menu and obtains a selection from the user. The function must return the selection as an integer. Therefore, it must refuse input from the user until an integer has been typed in.

The second place an integer is required is the guess. Do not accept a guess from the user unless an integer is typed at the keyboard. That is, ask him/her to try again if they enter something other than an integer.

You'll need to use a while loop to operate the menu driven interface. There are three choices:

0: exit

1: guess

2. get a new secret number

This is what I have so far for this assignment:

def main_menu():

print ("")

print (" Guessing Game ")

print ()

print ("Main Menu")

print ("1: Guess ")

print ("2. Get a new secret number ")

print ("0. Exit")

print (" ")

print ()
return int(input("Please enter your selection: "))

  
loop = 1

choice = 0

while loop == 1:

choice = main_menu()

if choice == 1:

print()

guess = input("Please enter your guess: ")

print()

try:

guess =int(guess)

except ValueError:

print(guess, "needs to be a number!")

print()

return int(input)

if guess == 5:

print("You win!")

elif guess > 5:

print("Too High")

elif guess < 5:

print("Too Low")
  
print ("Thanks for Playing")

However I am getting a 'return outside function' error. What I am doing wrong, and is there anything else that needs fixing thank you.

Explanation / Answer

NOTE: I have fixed the error in your code and the issue is fixed. Please check and let me know if you face any issues. I will revert back within 24 hours.

Explanation of the error:
You are getting return outside of the function because you have used return statement inside Except block which is not valid. Except block is there to only catch exceptions occurred in a try block. I have modified your code so it works correctly according to your logic.

Code:
http://pasted.co/ac0bcb43

Code output screenshot:
https://pasteboard.co/GDcs8l6W.png