简单的摇滚,纸张,剪刀游戏[Python]

时间:2015-08-27 15:59:31

标签: python if-statement random python-2.x

我正在尝试编写一个简单的RPS游戏,但我无法让程序显示游戏结果。请注意,我是编程的初学者,我已经从我之前学到的东西中写了所有这些,所以它可能不是写这样一个游戏最有效的方法。

import random
rps = ['rock', 'paper', 'scissors']
rps2 = (random.choice(rps))

print 'Welcome to RPS'
print 'Press 1 to pick Rock'
print 'Press 2 to pick Paper'
print 'Press 3 to pick Scissors'
print 'Press 4 to quit.'


while True:
    game = int(raw_input('What do you pick? '))
    if game == 1:
        print 'You picked rock.'
        print 'The computer picked...' + (random.choice(rps))
        if rps2 == [0]:
            print 'It\'s a tie!'
        elif rps2 == [1]:
            print 'You lose!'
        elif rps2 == [2]:
            print 'You win!'
    elif game == 2:
        print 'You picked paper.'
        print 'The computer picked...' + (random.choice(rps))
        if rps2 == [0]:
            print 'You win!'
        elif rps2 == [1]:
            print 'It\'s a tie!'
        elif rps2 == [2]:
            print 'You lose!'
    elif game == 3:
        print 'You picked scissors.'
        print 'The computer picked...' + (random.choice(rps))
    if rps2 == [0]:
        print 'You lose!'
    elif rps2 == [1]:
        print 'You win!'
    elif rps2 == [2]:
        print 'It\'s a tie!'
    elif game == 4:
        print 'Thank you for playing!'
        break
    else:
        continue

2 个答案:

答案 0 :(得分:0)

你的计划没有多大意义;这条线

    print 'The computer picked...' + (random.choice(rps))

rps中选择一个随机元素,因此'rock'paperscissors。但是你只是打印那个元素,而不是保存它。

然后你去比较第一次问你的用户之前随机选择的元素:

    if rps2 == [0]:

这没有意义。您将一个字符串(rps的一个元素)与一个列表进行比较,其中0为唯一元素。

您可能想做的事情就像是

while True:
    game = int(raw_input('What do you pick? '))
    if game == 1:
        print 'You picked rock.'
        computerpick = random.choice(rps)
        print 'The computer picked...' + computerpick
        if computerpick == 'rock':
            print 'It\'s a tie!'
        elif computerpick == 'paper':
            print 'You lose!'
        elif rps2 == 'scissors':
            print 'You win!'
  ...

答案 1 :(得分:0)

问题是,在比较中:

    if rps2 == [0]:
        print 'It\'s a tie!'
    elif rps2 == [1]:
        print 'You lose!'
    elif rps2 == [2]:
        print 'You win!'

你应该写它

    if rps2 == rps[0]:
        print 'It\'s a tie!'
    elif rps2 == rps[1]:
        print 'You lose!'
    elif rps2 == rps[2]:
        print 'You win!'

因为您想要访问rps列表。

那里你也有错误的缩进。我把它修改为:

import random
rps = ['rock', 'paper', 'scissors']
rps2 = (random.choice(rps))

print 'Welcome to RPS'
print 'Press 1 to pick Rock'
print 'Press 2 to pick Paper'
print 'Press 3 to pick Scissors'
print 'Press 4 to quit.'


while True:
    game = int(raw_input('What do you pick? '))
    if game == 1:
        print 'You picked rock.'
        print 'The computer picked...' + (random.choice(rps))
        if rps2 == rps[0]:
            print 'It\'s a tie!'
        elif rps2 == rps[1]:
            print 'You lose!'
        elif rps2 == rps[2]:
            print 'You win!'
    elif game == 2:
        print 'You picked paper.'
        print 'The computer picked...' + (random.choice(rps))
        if rps2 == rps[0]:
            print 'You win!'
        elif rps2 == rps[1]:
            print 'It\'s a tie!'
        elif rps2 == rps[2]:
            print 'You lose!'
    elif game == 3:
        print 'You picked scissors.'
        print 'The computer picked...' + (random.choice(rps))
        if rps2 == rps[0]:
            print 'You lose!'
        elif rps2 == rps[1]:
            print 'You win!'
        elif rps2 == rps[2]:
            print 'It\'s a tie!'
    elif game == 4:
        print 'Thank you for playing!'
        break
    else:
        continue

测试。