我不确定如何将这两段代码和一个菜单放在一起,以便能够选择要运行的菜单,类似于以下内容:
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.")
快速简便的方法是仅创建一个运行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] 删除。
我来说两句