Please advise how to put some exception handling around code for ValueError for invalid literal instead of integer while prompted for menu select? import random p1 = 0 p2 = 0 cpu = 0 tie = 0 def main(): menuSelect = "" print("Welcome to Rock, Paper, Scissors!\n") # main menu print("Main Menu") print("1. See the rules") print("2. Play against the computer") print("3. Play a two player game") print("4. Quit\n") menuSelect = int(input("Please make choice 1-4:\t")) while menuSelect < 1 or menuSelect > 4: print("The selection provided is invalid.\n") menuSelect = int(input("Please select one of the four options:\t")) if menuSelect == 1: rules() elif menuSelect == 2: onePlayer() elif menuSelect == 3: twoPlayer() elif menuSelect == 4: endGame() # display the rules to the user def rules(): print("\n\nRules") print("________________________") print("Paper Covers Rock") print("Rock Smashes Scissors") print("Scissors Cut Paper\n") main() # one player mode def onePlayer(): p1 = 0 p2 = 0 cpu = 0 tie = 0 again = "" player = False print("\nPlayer VS Computer") while player == False: print("Select a Weapon!") print("1. Rock") print("2. Paper") print("3. Scissors") print("4. Return to Main Menu") player = int(input("\nWhat is your weapon of choice(1-4):\t")) #computer = WEAPON[randint(0,2)] #temporary computer = random.randint(1, 3) if player == computer: print("It's a tie!\n") tie = tie + 1 elif player == 1: if computer == 2: print("Paper covers rock! You lose!\n") cpu = cpu + 1 else: print("Rock smashes scissors. You win!\n") p1 = p1 + 1 elif player == 2: if computer == 3: print("Scissors cut paper! You lose!\n") cpu = cpu + 1 else: print("Paper covers rock. You win!\n") p1 = p1 + 1 elif player == 3: if computer == 1: print("Rock smashes scissors! You lose!\n") cpu = cpu + 1 else: print("Scissors cut paper. You win!\n") p1 = p1 + 1 else: print("invalid input") print("Scores for this session:\n") print("Ties:\t", tie) print("Computer:\t", cpu) print("Player 1:\t", p1) again = input("Would you like to play again? Yes or No\n") again = again.lower() if again == "yes" or again == "y": player = False else: player = True main() def twoPlayer(): p12 = 0 p2 = 0 tie2 = 0 again = "" player1 = False player2 = 0 print("\nPlayer VS Computer") while player1 == False: print("Select a Weapon!") print("1. Rock") print("2. Paper") print("3. Scissors") print("4. Return to Main Menu") player1 = int( input("\nPlayer 1 what is your weapon of choice(1-4):\t")) player2 = int( input("\n\nPlayer 2 what is your weapon of choice(1-4):\t")) if player1 == player2: print("It's a tie!\n") tie2 = tie2 + 1 elif player1 == 1: if player2 == 2: print("Paper covers rock! You lose!\n") p2 = p2 + 1 else: print("Rock smashes scissors. You win!\n") p12 = p12 + 1 elif player1 == 2: if player2 == 3: print("Scissors cut paper! You lose!\n") p2 = p2 + 1 else: print("Paper covers rock. You win!\n") p12 = p12 + 1 elif player1 == 3: if player2 == 1: print("Rock smashes scissors! You lose!\n") p2 = p2 + 1 else: print("Scissors cut paper. You win!\n") p12 = p12 + 1 else: print("invalid input") print("Scores for this session:\n") print("Ties:\t", tie2) print("Player 1:\t", p12) print("Player 2:\t", p2) again = input("Would you like to play again? Yes or No\n") again = again.lower() if again == "yes" or again == "y": player1 = False else: player1 = True main() def endGame(): return; main()

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Please advise how to put some exception handling around code for ValueError for invalid literal instead of integer while prompted for menu select?

import random

p1 = 0
p2 = 0
cpu = 0
tie = 0


def main():
menuSelect = ""
print("Welcome to Rock, Paper, Scissors!\n")

# main menu
print("Main Menu")
print("1. See the rules")
print("2. Play against the computer")
print("3. Play a two player game")
print("4. Quit\n")

menuSelect = int(input("Please make choice 1-4:\t"))

while menuSelect < 1 or menuSelect > 4:
print("The selection provided is invalid.\n")
menuSelect = int(input("Please select one of the four options:\t"))

if menuSelect == 1:
rules()
elif menuSelect == 2:
onePlayer()
elif menuSelect == 3:
twoPlayer()
elif menuSelect == 4:
endGame()


# display the rules to the user
def rules():

print("\n\nRules")
print("________________________")
print("Paper Covers Rock")
print("Rock Smashes Scissors")
print("Scissors Cut Paper\n")
main()


# one player mode
def onePlayer():
p1 = 0
p2 = 0
cpu = 0
tie = 0
again = ""
player = False

print("\nPlayer VS Computer")

while player == False:
print("Select a Weapon!")
print("1. Rock")
print("2. Paper")
print("3. Scissors")
print("4. Return to Main Menu")
player = int(input("\nWhat is your weapon of choice(1-4):\t"))

#computer = WEAPON[randint(0,2)]
#temporary
computer = random.randint(1, 3)

if player == computer:
print("It's a tie!\n")
tie = tie + 1
elif player == 1:
if computer == 2:
print("Paper covers rock! You lose!\n")
cpu = cpu + 1
else:
print("Rock smashes scissors. You win!\n")
p1 = p1 + 1
elif player == 2:
if computer == 3:
print("Scissors cut paper! You lose!\n")
cpu = cpu + 1
else:
print("Paper covers rock. You win!\n")
p1 = p1 + 1
elif player == 3:
if computer == 1:
print("Rock smashes scissors! You lose!\n")
cpu = cpu + 1
else:
print("Scissors cut paper. You win!\n")
p1 = p1 + 1
else:
print("invalid input")

print("Scores for this session:\n")
print("Ties:\t", tie)
print("Computer:\t", cpu)
print("Player 1:\t", p1)

again = input("Would you like to play again? Yes or No\n")
again = again.lower()

if again == "yes" or again == "y":
player = False
else:
player = True
main()


def twoPlayer():
p12 = 0
p2 = 0
tie2 = 0
again = ""
player1 = False
player2 = 0

print("\nPlayer VS Computer")

while player1 == False:
print("Select a Weapon!")
print("1. Rock")
print("2. Paper")
print("3. Scissors")
print("4. Return to Main Menu")
player1 = int(
input("\nPlayer 1 what is your weapon of choice(1-4):\t"))
player2 = int(
input("\n\nPlayer 2 what is your weapon of choice(1-4):\t"))

if player1 == player2:
print("It's a tie!\n")
tie2 = tie2 + 1
elif player1 == 1:
if player2 == 2:
print("Paper covers rock! You lose!\n")
p2 = p2 + 1
else:
print("Rock smashes scissors. You win!\n")
p12 = p12 + 1
elif player1 == 2:
if player2 == 3:
print("Scissors cut paper! You lose!\n")
p2 = p2 + 1
else:
print("Paper covers rock. You win!\n")
p12 = p12 + 1
elif player1 == 3:
if player2 == 1:
print("Rock smashes scissors! You lose!\n")
p2 = p2 + 1
else:
print("Scissors cut paper. You win!\n")
p12 = p12 + 1
else:
print("invalid input")

print("Scores for this session:\n")
print("Ties:\t", tie2)
print("Player 1:\t", p12)
print("Player 2:\t", p2)

again = input("Would you like to play again? Yes or No\n")
again = again.lower()

if again == "yes" or again == "y":
player1 = False
else:
player1 = True
main()

def endGame():
return;


main()

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY