如何添加计数?

时间:2015-08-16 10:45:47

标签: python

我需要一些帮助来为以下代码创建一个计数器:

import random

def number_to_name(selection):
    # convert number to a name using if/elif/else
    if   selection == 0: return "rock"
    elif selection == 1: return "Spock"
    elif selection == 2: return "paper"
    elif selection == 3: return "lizard"
    elif selection == 4: return "scissors"
    else:             return None

def name_to_number(name):
    # convert name to number using if/elif/else
    if   name == "rock":     return 0
    elif name == "Spock":    return 1
    elif name == "paper":    return 2
    elif name == "lizard":   return 3
    elif name == "scissors": return 4
    else:                    return None


def rpsls(name): 
    # convert name to player_number using name_to_number

    # compute random guess for comp_number using random.randrange()

    # compute difference of player_number and comp_number modulo five

    # use if/elif/else to determine winner

    # convert comp_number to name using number_to_name

    # print results
    player_number = name_to_number(name)
    comp_number = random.randrange(0, 5)
    difference = (player_number - comp_number)  % 5

    print "\nComputer 2 chooses", name
    print "Computer 1 chooses", number_to_name(comp_number)

    #print "Score was:", difference # XXX

    if   difference == 0: print "Computer 2 and Computer 1 tie!"
    elif difference <= 2: print "Computer 2 wins!"
    else:                 print "Computer 1 wins!"

    print

# test code
print                         "Welcome to..."
import sys
import time
line = "Rock! Paper! Scissors! Lizard! Spock!."
for char in line:
     sys.stdout.write(char)
     time.sleep(0.02)
print "\n<Follow the enter key prompts!>"
raw_input("\n\nPress the enter key to continue.")

rpsls("rock")
raw_input("\n\nPress the enter key to continue.")
rpsls("Spock")
raw_input("\n\nPress the enter key to continue.")
rpsls("paper")
raw_input("\n\nPress the enter key to continue.")
rpsls("lizard")
raw_input("\n\nPress the enter key to continue.")
rpsls("scissors")
raw_input("\n\nPress the enter key to continue.")
rpsls("rock")
raw_input("\n\nPress the enter key to continue.")
rpsls("Spock")
raw_input("\n\nPress the enter key to continue.")
rpsls("paper")
raw_input("\n\nPress the enter key to continue.")
rpsls("lizard")
raw_input("\n\nPress the enter key to continue.")
rpsls("scissors")

raw_input('\n\nPress the enter key to exit')

在每个单独的游戏(岩石剪刀蜥蜴spock)之后,我需要一个计算来计算每个计算机的胜利损失和抽奖。

e.g. Computer 2 chooses rock 
Computer 1 chooses paper 
Computer 1 wins!

        > <Computer 1 : Draws = 1
        >               Wins = 3
        >               Losses = 2
        > 
        > Computer 2 : Draws = 1
        >              Wins = 2
        >              Losses = 3>

或者沿着这些方向的东西,越简单越好。

请帮助,我刚刚开始使用python,非常感谢所有帮助。

0 个答案:

没有答案