如何将两个代码/程序放在一起?

锌锌

我不确定如何将这两段代码和一个菜单放在一起,以便能够选择要运行的菜单,类似于以下内容:

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:

或类似的规定。我对编码非常陌生,因此我不知道该如何处理。所有帮助将不胜感激:)

如果有帮助,可以使用以下两个代码:代码1(由计算机生成);

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()

代码2(您与计算机);

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

快速简便的方法是仅创建一个运行menu1.py的菜单,该菜单根据用户的输入运行code1.py或code2.py。

#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'

将menu.py,code1.py和code2.py都放在同一目录中,然后运行menu.py。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将两个阵列放在一起比较,如果它们具有相同的OWNERID

如何将两个二维数组“粘贴”在一起?

如何将两个非字符串加在一起...?

如何将两个ng-models值绑定在一起

如何将两个NodeJS docker容器连接在一起?

如何将两个视图堆叠在一起?

如何将两个python脚本Pi3 Gpio放在一起

如何将两个python代码粘合(缝合)在一起?

如何将两个求和查询加在一起?

如何将两个打字稿泛型联系在一起

如何将这两个查询结合在一起?(MySQL)

如何将两个嵌入在一起?不和谐

如何将两个盖茨比站点合并在一起

如何将两个图像连接在一起?

如何将两个jquery对象包装在一起?

如何将两个填充区域地块放在一起?

如何将两个实体对象连接在一起?

如何将两个离子应用程序连接在一起?

如何将两个项目的两个 application.yml 配置放在一起?

如何将两个 TextBlock 'Runs' 放在一起

如何将两个任务集合链接在一起?

如何将两个css类名放在一起,在页面呈现后删除第二个?

如何将两个列表的元素打印在一起

如何将两个表连接在一起以创建 JSON | PHP

如何将两个列表连接在一起但频率不同

如何将这两个配置部分连接在一起?

如何将两个或多个元素放在一起并溢出?

如何将两个列表相同位置的元素放在一起组成一个列表列表?

如何将左右两个 QFrame 放在一起?