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()
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()
Step by step
Solved in 3 steps