Python程序没有按预期执行(初学者Rock Paper Scissor)

时间:2014-11-14 13:05:20

标签: python

import random
print "Welcome to Rock, Papeeer, Scissor!"
name = raw_input("What's your name?")
print "Welcome %s" % name
rounds = raw_input("How many rounds you want to play?")
rps_choice = ['R', 'P', 'S']

def rps(r,p,s):
    x = 0 
    hs = 0
    cs = 0
    draw = 0
    while x < int(rounds):
        computer_choice = random.choice (rps_choice)
        human_choice = raw_input("Choose (R)ock, (P)aper, or (S)cissor?")
        human_choice.upper()

        print "Computer choose : %s" %computer_choice
        print computer_choice
        if computer_choice == 'r' and human_choice == 's' or computer_choice == 's' and human_choice == 'p' or computer_choice == 'p' and human_choice == 'r':
            print "computer wins"
            print "Ha Ha Ha looser, Sachin fails."
            cs = cs + 1
            print "Computer score : %d" % cs 
            print "Human score : %d"  % hs
        elif computer_choice == human_choice:
            print "Its a draw"
            draw = draw + 1
        else :
            print "You won %s" % name
            hs = hs + 1
            print "Computer score %d:" % cs 
            print "Human score : %d" % hs
            x = x + 1
    print "Final score is Computer : %d and %s : %d" % (cs, name, hs)
    if hs > cs:
        print "You won, Sachin is the greatest player ever born."
    elif hs == cs:
        print "Its a draw"
    else:
        print "Muhahahaha Sachin ki aisi ki taisi, you lost."
rps('R', 'P', 'S')

现在,它运行正常,但无论用户提供什么输入,用户总是获胜。甚至没有平局(即使选择与预期相同)。例如:

Welcome to Rock, Papeeer, Scissor!
What's your name?Alpha
Welcome Alpha
How many rounds you want to play?3
Choose (R)ock, (P)aper, or (S)cissor?s
Computer choose : R
R
You won Alpha
Computer score 0:
Human score : 1
Choose (R)ock, (P)aper, or (S)cissor?p
Computer choose : P
P
You won Alpha
Computer score 0:
Human score : 2
Choose (R)ock, (P)aper, or (S)cissor?r
Computer choose : P
P
You won Alpha
Computer score 0:
Human score : 3
Final score is Computer : 0 and Alpha : 3
You won, Sachin is the greatest player ever born.
bash-4.2$ 

Well计算机应该赢得第一轮和最后一轮,而第二轮应该是平局,但无论用户总是在这里获胜。

现在我设法通过查看其他类似程序设法运行此程序(仅通过stackoverflow),但我的代码在此过程中已经彻底改变,所以我正在寻找&#34;导致错误的原因&# 34;在我的程序中,而不是问题的解决方案。

2 个答案:

答案 0 :(得分:4)

字符串是不可变的,str.upper()方法返回 new 字符串对象。你在这里忽略了这一点:

human_choice.upper()

您想存储结果:

human_choice = human_choice.upper()

但是,您将继续针对小写字符测试所有选项:

if computer_choice == 'r' and human_choice == 's' or computer_choice == 's' and human_choice == 'p' or computer_choice == 'p' and human_choice == 'r':

你需要在这里下定决心。要么小写所有输入,要么大写所有输入,然后在测试中也使用适当的大小写!

不是测试每个可能的组合,而是使用映射查找每个用户选择的结果:

beats = {'R': 'S', 'S': 'P', 'P': 'R'}

if beats[human_choice] == computer_choice:
    # human wins!
elif beats[computer_choice] == human_choice
    # computer wins!
else:
    # assume a draw

beats['R'](摇滚)返回'S',剪刀等。这很容易扩展,所以你也可以玩Rock, Paper, Scissors, Lizard, Spock,而不必用手写出所有10个获胜组合:

beats {
    'rock': {'lizard', 'scissors'},
    'paper': {'rock', 'spock'},
    'scissors': {'paper', 'lizard'},
    'lizard': {'spock', 'paper'},
    'spock': {'scissors', 'rock'},
}

if computer_choice in beats[human_choice]:
    # human won
# etc.

答案 1 :(得分:1)

upper不会修改字符串,您需要使用返回的值将其分配给某些内容。对于这一行

human_choice.upper()

你可以做到

human_choice = human_choice.upper()

例如,看一下这个演示

>>> letter = 'a'
>>> letter
'a'
>>> letter.upper()
'A'
>>> letter
'a'           # Notice that letter has not been modified
>>> letter = letter.upper()
>>> letter
'A'           # Now it has
相关问题