1

当我刚开始时,我正在制作一个石头剪刀布游戏作为一个小项目,我必须经常使用相同的代码。最好的方法是什么,因为目前我已经将它复制并粘贴到了整个地方。它看起来很不整洁,而且我在验证输入时遇到了循环问题,不得不全部更改它是一件很痛苦的事情。

user_continue = raw_input("Would you like to play again? Y/N: ")
user_continue = user_continue.upper()
#Yes or no continue for the continue while loop to continue working until correct user input.
y_n_continue = False
while y_n_continue == False:
    if user_continue == "Y" or user_continue == "YES" or user_continue == "N" or user_continue == "NO":
        if user_continue == "Y" or user_continue == "YES":
            continue_game = True
            y_n_continue = True
        elif user_continue == "N" or user_continue == "NO":
            continue_game = False
            y_n_continue = True
        else:
            print "Press Y or N"
            y_n_continue = False
    else:
        print ""

如果我添加整个代码可能会更容易(有了修复,感谢 Anton。目前我收到错误 - TypeError:'bool' object is not callable。

我基本上是想让它循环游戏只要用户想要,同时还要验证输入以使一切尽可能防弹。

编辑 2 - 这是新代码,我有一些测试数据。

当我启动它时,系统会提示您在开始时输入 y/n。

您还必须在每场比赛后输入 y 或 n 两次。

如果您将“错误”数据输入到石头/纸/剪刀选择中,它将进入 y/n 选择

import random


def continue_game():
    while True:
        user_continue = raw_input("Would you like to play again? Y/N: ").upper()
        if user_continue in ["Y", "YES", "N", "NO"]:
            return user_continue in ["Y", "YES"]
        else:
            print "Press Y or N"



while continue_game():

    #computers choice of rock, paper or scissors
    computer_input = ["ROCK", "PAPER", "SCISSORS"]
    computer_choice = random.choice(computer_input)
    #users choice or rock, paper or scissors
    user_input = raw_input("Choose rock, paper or scissors: ")
    #Turns user input to upper case.
    user_choice = user_input.upper()
    if user_choice == "ROCK" or user_choice == "PAPER" or user_choice == "SCISSORS":
        #Computer = ROCK
        if computer_choice == "ROCK":
            #user = ROCK
            if user_choice == "ROCK":
                print "You have chosen: " + user_choice
                print "The computer has chosen: " + computer_choice
                print "You draw!"

                #replay?
                if continue_game():
                    print "continue"
                else:
                    continue_game = False



            #user = PAPER
            elif user_choice == "PAPER":
                print "You have chosen: " + user_choice
                print "The computer has chosen: " + computer_choice
                print "You win!"
                #replay?
                if continue_game():
                    print "continue"
                else:
                    continue_game = False


            #user = SCISSORS   
            elif user_choice == "SCISSORS":
                print "You have chosen: " + user_choice
                print "The computer has chosen: " + computer_choice
                print "You lose!"
                #replay?
                if continue_game():
                    print "continue"
                else:
                    continue_game = False


        #Computer = PAPER
        elif computer_choice == "PAPER":
            #user = ROCK
            if user_choice == "ROCK":
                print "You have chosen: " + user_choice
                print "The computer has chosen: " + computer_choice
                print "You lose!"
                #replay?
                if continue_game():
                    print "continue"
                else:
                    continue_game = False

            #user = PAPER
            elif user_choice == "PAPER":
                print "You have chosen: " + user_choice
                print "The computer has chosen: " + computer_choice
                print "You draw!"
                if continue_game():
                    print "continue"
                else:
                    continue_game = False


            #user = SCISSORS   
            elif user_choice == "SCISSORS":
                print "You have chosen: " + user_choice
                print "The computer has chosen: " + computer_choice
                print "You win!"
                #replay?
                if continue_game():
                    print "continue"
                else:
                    continue_game = False

        #Computer = SCISSORS
        elif computer_choice == "SCISSORS":
            #user = ROCK
            if user_choice == "ROCK":
                print "You have chosen: " + user_choice
                print "The computer has chosen: " + computer_choice
                print "You win!"
                #replay?
                if continue_game():
                    print "continue"
                else:
                    continue_game = False

            #user = PAPER
            elif user_choice == "PAPER":
                print "You have chosen: " + user_choice
                print "The computer has chosen: " + computer_choice
                print "You lose!"
                #replay?
                if continue_game():
                    print "continue"
                else:
                    continue_game = False

            #user = SCISSORS   
            elif user_choice == "SCISSORS":
                print "You have chosen: " + user_choice
                print "The computer has chosen: " + computer_choice
                print "You draw!"
                #replay?
                if continue_game():
                    print "continue"
                else:
                    continue_game = False

        else:
            print "Something has gone wrong."
    else:
        print "Are you sure you entered that correctly?"

输出:

Would you like to play again? Y/N: y

Choose rock, paper or scissors: rock

You have chosen: ROCK

The computer has chosen: PAPER

You lose!

Would you like to play again? Y/N: y

continue

Would you like to play again? Y/N: y

Choose rock, paper or scissors: paper

You have chosen: PAPER

The computer has chosen: ROCK

You win!

Would you like to play again? Y/N: wer

Press Y or N

Would you like to play again? Y/N: dfg

Press Y or N

Would you like to play again? Y/N: y

continue

Would you like to play again? Y/N: y

Choose rock, paper or scissors: test

Are you sure you entered that correctly?

Would you like to play again? Y/N: y

Choose rock, paper or scissors: rock

You have chosen: ROCK

The computer has chosen: SCISSORS

You win!

Would you like to play again? Y/N: n

exit

Would you like to play again? Y/N: n

>>> 

我知道我很痛苦,但这一切都值得赞赏。

4

3 回答 3

4

您可以从代码中创建一个函数并返回continue_game变量的值。这是包装在函数中的代码的缩小版本,以及它的用法示例:

def continue_game():
    while True:
        user_continue = raw_input("Would you like to play again? Y/N: ").upper()
        if user_continue in ["Y", "YES", "N", "NO"]:
            return user_continue in ["Y", "YES"]
        else:
            print "Press Y or N"

if continue_game():
    print "continue"
else:
    print "exit"

更新:关于您的完整代码,要修复错误,您需要删除以下行:

continue_game = True

并替换:

while continue_game == True:

和:

while continue_game():
于 2012-10-14T09:25:30.830 回答
0

首先你应该避免像 while var == False 使用 while not var 之类的东西:

同样的事情:

"如果 user_continue == "Y" 或 user_continue == "YES" 或 user_continue == "N" 或 user_continue == "NO":"

做类似的事情:

“如果 user_continue.upper() 在 "YESNO"

有更好的方法吗?反而 :)

我认为询问玩家是否想再次玩的正确方法是制作一个“游戏”函数并在最后调用它

这里的例子:http: //pastebin.com/0jQtwGdi

于 2012-10-14T10:26:54.120 回答
0

一旦你弄清楚这个代表游戏规则的表格:

      | ROCK2 | PAPR2 | SCIS2 |
------+-------+-------+-------|
ROCK1 |    0  |     2 |     1 |
PAPR1 |    1  |     0 |     2 |
SCIS1 |    2  |     1 |     0 |

1 = column wins, 2 = row wins, 0 = draw

通过使用字典和元组的索引以及一些异常处理,您的整个程序可以缩小很多:

import random

m = dict(
        rock     = dict(rock=0, paper=2, scissors=1),
        paper    = dict(rock=1, paper=0, scissors=2),
        scissors = dict(rock=2, paper=1, scissors=0)
    )

while True:

    try:
        continue_game = {'n': False, 'y': True}[
            raw_input("Would you like to play again? Y/N: ")[0].lower()
        ]
    except:
        print "Press Y or N"
        continue # next loop

    if not continue_game: break

    human = raw_input("Choose rock, paper or scissors: ").lower()
    computer = random.choice(m.keys())

    print "You have chosen:", human
    print "The computer has chosen:", computer
    print ("You draw!", "You lose!", "You win!")[ m[computer][human] ]

一开始您可能会发现它有点棘手,但是这样设计的代码可以确保您不会重复自己,并且它比在您的情况下使用 if-then-else 结构化的代码更具可读性(和可维护性)。

于 2012-10-14T10:33:00.230 回答