-1

请有人告诉我如何做一个基本的评分系统来进入这个代码。

import random
print 'Welcome to Rock, paper, scissors!'
firstto=raw_input('How many points do you want to play before it ends? ')

playerscore=+0
compscore=+0

while True:
    choice = raw_input ('Press R for rock, press P for paper or press S for scissors, CAPITALS!!! ')

    opponent = random.choice(['rock', 'paper' ,'scissors' ])

    print 'Computer has chosen', opponent

    if choice == 'R' and opponent == "rock":
        print 'Tie'
        playerscore = playerscore+0
        compscore = compscore+0

    elif choice == 'P' and opponent == "paper":
        print 'Tie'
        playerscore = playerscore+0
        compscore = compscore+0

    elif choice == 'S' and opponent == "scissors":
        print 'Tie'
        playerscore = playerscore+0
        compscore = compscore+0

    elif choice == 'R' and opponent == "paper":
        print 'CPU Wins'
        playerscore = playerscore+0
        compscore = compscore+1

    elif choice == 'P' and opponent == "scissors":
        print 'CPU Wins'
        playerscore = playerscore+0
        compscore = compscore+1

    elif choice == 'S' and opponent == "rock":
        print 'CPU Wins'
        playerscore = playerscore+0
        compscore = compscore+1


    elif choice == 'P' and opponent == "rock":
            print 'You Win'
            playerscore = playerscore+1 
            compscore = compscore+0

    elif choice == 'S' and opponent == "paper":
            print 'You Win'
            playerscore = playerscore+1
            compscore = compscore+0

    elif choice == 'R' and opponent == "scissors":
            print 'You Win'
            playerscore = playerscore+1
            compscore = compscore+0


    print 'Player score is',playerscore
    print 'Computer score is',compscore

    if playerscore == firstto:
        'You won the game :)'
        exit()
    elif compscore == firstto:
        'You lost the game :('
        exit()
4

3 回答 3

3

问题出raw_input在第 3 行。raw_input总是返回一个字符串,而您需要的是一个 int。如果将第 3 行更改为:

firstto = int(raw_input('How many points do you want to play before it ends? '))

您的代码将起作用。

"hello"要清理用户输入(这样当用户输入而不是时,您的代码不会崩溃),您可以将调用5包装到,语句中。raw_inputtryexcept

例如:

valid_input = False # flag to keep track of whether the user's input is valid.
while not valid_input:
    firstto_str = raw_input('How many points do you want to play before it ends? ')
    try:
        # try converting user input to integer
        firstto = int(firstto_str)
        valid_input = True
    except ValueError:
        # user input that cannot be coerced to an int -> raises ValueError.
        print "Invalid input, please enter an integer."

顺便说一句,您的代码陷入了无限循环,因为您使用的是由提供的字符串raw_input与整数进行比较。这将始终返回 False:

>>> "5" == 5
False
于 2013-09-12T11:33:08.450 回答
1

有很多方法可以优化此代码,但您的直接问题是raw_input点数的输入。这将返回一个字符串,而您需要一个int. 把它包起来int(),你会没事的。也就是说,直到有人输入一个无法解析的东西。

firstto = int(raw_input('How many points do you want to play before it ends? '))

编辑:如果您有兴趣,我已经尝试过优化您的代码(不走极端):

import random

what_beats_what = [('R', 'S'), ('S', 'P'), ('P', 'R')]
choices = {'R': 'Rock', 'P': 'Paper', 'S': 'Scissors'}


def outcome(player_a, player_b):
    for scenario in what_beats_what:
        if player_a == scenario[0] and player_b == scenario[1]:
            return 'A'
        elif player_b == scenario[0] and player_a == scenario[1]:
            return 'B'


print 'Welcome to Rock, paper, scissors!'

score_to_win = 0

while True:
    try:
        score_to_win = int(raw_input('How many points do you want to play before it ends? '))
        if score_to_win > 0:
            break
    except ValueError:
        pass

    print 'Try again, with a positive integer.'

human_score = 0
cpu_score = 0

while human_score < score_to_win and cpu_score < score_to_win:
    human_choice = ''
    while True:
        human_choice = raw_input('Press R for rock, press P for paper or press S for scissors: ').upper()
        if human_choice in choices:
            break
        else:
            print 'Try again ...'

    cpu_choice = random.choice(choices.keys())
    print 'Computer has chosen: {0}'.format(choices[cpu_choice])

    result = outcome(human_choice, cpu_choice)

    if result == 'A':
        print "Human wins!"
        human_score += 1
    elif result == 'B':
        print "CPU wins!"
        cpu_score += 1
    else:
        print 'It is a tie!'

    print 'Human score is: {0}'.format(human_score)
    print 'CPU score is: {0}'.format(cpu_score)

print 'You won the game :)' if human_score > cpu_score else 'You lost the game :('
于 2013-09-12T11:45:02.190 回答
0

修改您的第一个 raw_input 值。你在那里得到一个字符串。

为了获得更好的结果,还要验证输入:-)

while 1:
    firstto=raw_input('How many points do you want to play before it ends? ')
    if firstto.isdigit():
        firstto = int(firstto)
        break
    else:
        print "Invalid Input. Please try again"

这将只接受带有数字的字符串。即使有人输入“5.0”,它也会被忽略。

要更好地阅读 raw_input,请单击此处。要了解有关内置字符串方法的更多信息,请阅读此处

PS:这与问题无关。不过有点建议。您的代码可以变得更简单。如果您正在学习,请将此代码保留为 v0.1,并根据您的进度进行更新。

于 2013-09-12T12:02:18.247 回答