摇滚,纸张,剪刀......似乎无法让它正常工作

时间:2014-10-26 09:33:18

标签: python python-3.x

我的摇滚,纸张,剪刀游戏有点问题。我试图让for循环工作,但是当它打成平局时,它就不会打印我写的内容。

import random
#main function
def main():
    for game_count in range(5):
        answer = int(input("Enter 1 for Rock, 2 for Paper, 3 for Scissors: "))
        number = random.randrange(1,4)
        if answer == 1:
            position = "rock"
            print("You choose", position)
        elif answer == 2:
            position = "paper"
            print("You choose", position)
        elif answer == 3:
            position = "scissors"
            print("You choose", position)
        else:
            print("pick either 1, 2, or 3")
            return main()
#computer picks what to play    
        if number == 1:
            print("Computer chooses Rock")
        elif number == 2:
            print("Computer chooses Paper")
        elif number == 3:
            print("Computer chooses Paper")
        return number
#if there is a tie
        if answer == number:
            print("It's a tie. Go again")
main()

2 个答案:

答案 0 :(得分:1)

编写main函数是不必要的,以递归方式调用它来形成循环当然是个坏主意。

此计划可能会让您感兴趣。

from random import randrange
from string import capitalize

weapons = [ 'rock',   'paper', 'scissors' ]
actions = [ 'blunts', 'wraps', 'cuts' ]

games = 0

while True:

    answer, position = None, None

    while True:
        answer = int(input("\nEnter 1 for Rock, 2 for Paper, 3 for Scissors: "))
        answer -= 1
        position = weapons[answer]
        if position:
            break
        print("Pick either 1, 2, or 3")

    print("You choose %s" % position)

    number = randrange(3)
    cpos = weapons[number]
    print("Computer chooses %s" % cpos)

    outcome = (answer - number) % 3

    if outcome == 0:
        print("It's a tie. Go again")
    elif outcome == 1:
        print(capitalize("%s %s %s" % (position, actions[answer], cpos)))
        print("Congratulations, you win!")
    elif outcome == 2:
        print(capitalize("%s %s %s" % (cpos, actions[number], position)))
        print("Computer wins!")

    games += 1
    if games == 5:
        break

print("Thanks for playing")

答案 1 :(得分:0)

我喜欢Borodins answer,但我不认为它会遗漏原始代码(请务必仔细检查并仔细研究)。

让我们在这里提出4点:

  • 您不需要在python中创建main()函数
  • 不要以这种方式递归调用函数,请使用loop
  • 您将return number放在无效的地方,没有它,它应该正常工作
  • 您不需要手动检查id,您可以dictionary使用此

所以字典:

# You can easily replace this code:
if answer == 1:
    position = "rock"
    print("You choose", position)
elif answer == 2:
    position = "paper"
    print("You choose", position)
elif answer == 3:
    position = "scissors"
    print("You choose", position)

# By this
import random
positions = {
    1: 'rock',
    2: 'paper',
    3: 'scissors'
}

# Later in the code
if answer in positions:
    position = positions[answer]
    print('You choose', position)
else:
    # Invalid value

避免递归并使用循环:

position = None
while position is None:
    answer = int(input("Enter 1 for Rock, 2 for Paper, 3 for Scissors: "))

    if answer in positions:
        position = positions[answer]
        print('You choose', position)
    else:
        print('pick either 1, 2, or 3')

现在只需将其应用于您的代码:

import random
positions = {
    1: 'rock',
    2: 'paper',
    3: 'scissors'
}

for game_count in range(5):

    # Pick a value for player
    position = None
    while position is None:
        answer = int(input("Enter 1 for Rock, 2 for Paper, 3 for Scissors: "))

        if answer in positions:
            position = positions[answer]
            print('You choose', position)
        else:
            print('pick either 1, 2, or 3')


    # Computer picks what to play    
    number = random.randrange(1,4)
    print('Computer chooses', positions[number]) # Again, dictionary

    # NO return number here

    # if there is a tie
    if answer == number:
        print("It's a tie. Go again")
        continue

    # do more comparisons to decide who wins

我还假设您想要赢得5场胜利(重试平局),所以只需重写循环条件:

# Instead of
# for game_count in range(5):
games_to_go = 5
while games_to_go > 0:
    # ... 
    # if there is a tie
    if answer == number:
        print("It's a tie. Go again")
        continue

     # decrease number of games to go
     games_to_go -= 1