I'm not sure how to go about putting these two lots of code together with a menu to be able to select either one to run, like something similar to this:
Welcome to (etc etc)
What would you like to play?
All computer generated OR You vs computer?
Selection 1 or 2? Please enter your selection:
Or something along those lines. I'm very new to coding so I have no idea as to how I should approach this. All help would be greatly appreciated :)
Here are the two codes if that helps at all: Code 1 (Computer Generated);
import sys
import time
import random
#Build a list to convert move numbers to names
move_names = "rock Spock paper lizard scissors".split()
#Build a dict to convert move names to numbers
move_numbers = dict((name, num) for num, name in enumerate(move_names))
win_messages = [
"Computer 2 and Computer 1 tie!",
"Computer 1 wins!",
"Computer 2 wins!",
]
def rpsls(name):
# convert Computer 1 name to player_number
player_number = move_numbers[name]
# generate random guess Computer 2
comp_number = random.randrange(0, 5)
# compute difference modulo five to determine winner
difference = (player_number - comp_number) % 5
print "\nComputer 2 chooses", name
print "Computer 1 chooses", move_names[comp_number]
#print "Score was:", difference # XXX
#Convert difference to result number.
#0: tie. 1: Computer 1 wins. 2:Computer 2 wins
if difference == 0:
result = 0
elif difference <= 2:
result = 2
else:
result = 1
return result
def main():
banner = "! ".join([word.capitalize() for word in move_names]) + "!.\n"
print "Welcome to..."
for char in banner:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(0.02)
print "Rules!:"
print """\nScissors cuts Paper
Paper covers Rock
Rock crushes Lizard
Lizard poisons Spock
Spock smashes Scissors
Scissors decapitates Lizard
Lizard eats Paper
Paper disproves Spock
Spock vaporizes Rock
(and as it always has) Rock crushes scissors"""
print "\n<Follow the enter key prompts!>"
raw_input("\n\nPress the enter key to continue.")
#A list of moves for Computer 1
computer1_moves = [
"rock",
"Spock",
"paper",
"lizard",
"scissors",
"rock",
"Spock",
"paper",
"lizard",
"scissors",
]
#Create a list to hold the scores
scores = [0, 0, 0]
for name in computer1_moves:
result = rpsls(name)
scores[result] += 1
print result, win_messages[result], scores
raw_input("\n\nPress the enter key to continue.")
print "\nFinal scores"
print "Computer 1 wins:", scores[1]
print "Computer 2 wins:", scores[2]
print "Ties:", scores[0]
raw_input("\n\nPress the enter key to exit")
if __name__ == "__main__":
main()
Code 2 (You vs Computer);
import random
data = "rock", "spock", "paper", "lizard", "scissors"
print "Rules!:"
print """Scissors cuts Paper
Paper covers Rock
Rock crushes Lizard
Lizard poisons Spock
Spock smashes Scissors
Scissors decapitates Lizard
Lizard eats Paper
Paper disproves Spock
Spock vaporizes Rock
(and as it always has) Rock crushes scissors"""
def playgames():
tally = dict(win=0, draw=0, loss=0)
numberofgames = raw_input("How many games do you want to play? ")
numberofgames = int(numberofgames)
for _ in range(numberofgames):
outcome = playgame()
tally[outcome] += 1
print """
Wins: {win}
Draws: {draw}
Losses: {loss}
""".format(**tally)
def playgame():
choice = ""
while (choice not in data):
choice = raw_input("Enter choice (choose rock , paper , scissors , lizard, or spock):")
choice = choice.lower()
print "Player choice is:{}".format(choice)
player_number = data.index(choice)
computer_number = random.randrange(5)
print "Computer choice is: {}".format(data[computer_number])
difference = (player_number - computer_number) % 5
if difference in [1, 2]:
print "Player wins!"
outcome = "win"
elif difference == 0:
print "Player and computer tie!"
outcome = "draw"
else:
print "Computer wins!"
outcome = "loss"
return outcome
playgames()
raw_input("\n\nPress the enter key to exit.")