In python 2.7 I am trying to get the following code to work out so it give the u
ID: 3758379 • Letter: I
Question
In python 2.7 I am trying to get the following code to work out so it give the user a menu of 3 options to choose from. Option 0 is number guessing game, option 2 is rock,paper, scissors and option 3 is to quit. I need it to work so that once the user chooses and plays the game it will loop back to the menu until they choose the quit option. Can someone please review the following code and see if they can fix it. The code for the individual games works just fine but the main menu code doesnt work. I am using canopy for editing if it matters.
My code:
def MENU(access):
print "Menu:"
print " 0. Number Guessing Game"
print " 1. Rock, Paper, Scissors, Lizard, Spock"
print " 2. Quit"
access = int(raw_input("Make a selection from the above list: "))
return access
if access == 0:
import random
guessestaken=0
print 'Hello, my name is sheldor and I am thinking of a number between 1 and 100 and I want you to guess what it is.'
print 'Take a guess.'
number=random.randint(1,100)
while guessestaken<6:
guess=input()
guess=int(guess)
guessestaken=guessestaken+1
if guess<number:
print 'Your guess is too low.'
if guess>number:
print 'Your guess is too high.'
if guess==number:
break
if guess == number:
guessestaken=str(guessestaken)
print 'Good job! You guessed the number in',guessestaken,'guesses!'
if guess != number:
number=str(number)
print 'Nope. The number I was thinking of was',number
elif access==1:
# 0 - rock
# 1 - Spock
# 2 - paper
# 3 - lizard
# 4 - scissors
print 'Lets play rock, paper, lizard, scissors, spock! Please choose your move. Please choose from the following options: '
print 'Type 0 for rock.'
print 'Type 1 for spock.'
print 'Type 2 for paper.'
print 'Type 3 for lizard.'
print 'Type 4 for scissors.'
number=int(raw_input('Type your choice to start the game: '))
def number_to_name(number):
if number == 0:
return 'rock'
elif number == 1:
return 'Spock'
elif number == 2:
return 'paper'
elif number == 3:
return 'lizard'
else:
return 'scissors'
def name_to_number(name):
if name == 'rock':
return 0
elif name == 'Spock':
return 1
elif name == 'paper':
return 2
elif name == 'lizard':
return 3
else:
return 4 #scissors
import random
def rpsls(guess):
number = name_to_number(guess)
comp_number = random.randrange(0,(4))
difference = (number - comp_number) %5
if difference == 0:
result = "Player and computer tie!"
elif difference == 1 or difference == 2:
result = "Player wins!"
else:
result = "Computer wins!"
computer_guess = number_to_name(comp_number)
print " Player chooses", number_to_name(number)
print "Computer chooses", computer_guess
print result
rpsls(number)
else:
print 'You have quit the game. Goodbye!'
MENU()
Explanation / Answer
CODE :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.