1

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.")

2 Answers 2

0

A quick and easy hack is to just create a menu.py that runs code1.py or code2.py depending on the user input.

#menu.py

import os

print """
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:
"""

choice = raw_input()


if choice == '1':
    os.system('code1.py')
elif choice == '2':
    os.system('code2.py')
else:
    print 'invalid choice'

Put menu.py, code1.py and code2.py all in the same directory and then run menu.py.

0
0

Name your first script as script1.py. Now add this at the top of your second script

import script1

And the following code snippet at the end of your second script:

print("Welcome! What would you like to play?")
print("All computer generated OR You vs computer?")
print("Selection 1 or 2? Please enter your selection:")

while True:
    choice = int(input())
    if choice == 1:
        script1.main()
    elif choice == 2:
        playgames()
    else:
        print("Enter either 1 or 2!\n")

( I have used python 3 syntax but it should be no problem to convert them to python 2).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.