简体   繁体   English

我该如何在python中使我的猜彩色游戏工作?

[英]how can i make my guessing color game work in python?

I have made a simple guessing color game. 我做了一个简单的猜色游戏。 The aim of the game is to try and guess the color that has been entered by game master. 游戏的目的是尝试猜测游戏管理员输入的颜色。

I was wondering how to loop it, so that you were allowed a number of guesses, to guess what the game master entered. 我想知道如何循环它,以便让您有很多猜测,可以猜出游戏大师输入的内容。

Any help would be greatly appreciated 任何帮助将不胜感激

print"THIS IS GUESSING GAME""\n"
print"WELCOME""\n"

rsplay = "Q"

print"Game master, enter the colour that you want:"
colour1 = raw_input("")
colour2 = raw_input("")
colour3 = raw_input("")
colour4 = raw_input("")

print"colour set!""\n"
num_guess = raw_input("Set the number of guess:")

print ("\n" * 50)

playername = raw_input("Enter Your Name: ")

   print "Hello" ,playername, "!" "\n" "You have<",num_guess, ">guesses to                  `enter the colors correctly in the order as" "\n" "how it being entered. Let's     play."`

   trial = 0
   x = 0
   y = 0

   print"Enter guess number" ,trial, ":"
   guess1 = raw_input("")
   guess2 = raw_input("")
   guess3 = raw_input("")
   guess4 = raw_input("")



    while trial < num_guess:
         trial = trial + 1

if (guess1 == colour1):
 x = x + 1
else :
    if (guess2 == colour2):
     x = x + 1
    else :
        if (guess3 == colour3):
          x = x + 1
        else :
            if (guess4 == colour4):
              x = x + 1
            else:
                if (guess1 == colour2):
                    y = y + 1
                else :
                    if (guess1 == colour3):
                        y = y + 1
                    else :
                        if (guess1 == colour4):
                            y = y + 1
                        else :
                            if (guess2 == colour1):
                                y = y + 1
                            else :
                                if (guess2 == colour3):
                                    y = y + 1
                                else :
                                    if (guess2 == colour4):
                                        y = y + 1
                                    else :
                                        if (guess3 == colour1):
                                            y = y + 1
                                        else :
                                            if (guess3 == colour2):
                                                y = y + 1
                                            else :
                                                if (guess3 == colour4):
                                                    y = y + 1
                                                else :
                                                    if (guess4 == colour1):
                                                        y = y + 1
                                                    else :
                                                        if (guess4 == colour2):
                                                            y = y + 1
                                                        else :
                                                            if (guess4 == colour3):
                                                                y = y + 1
                                                            else:
                                                                     print "You have" ,x, "CORRECT and" ,y, "MISSED" "\n"



 print "You've won! Well done",playername,"! You Did it in",trial,"guesses." "\n"

 print "Do You want to play again ?",rsplay,"\n"
 if(rsplay == "Q"):
print "Bye..."
else :
if (rsplay == "P"):
    print "Play Again"

You set: 您设置:

num_guess = raw_input("Set the number of guess:")

Then you can do: 然后,您可以执行以下操作:

while num_guess > 0:
    guess = raw_input("")
    # game logic goes here
    num_guess = num_guess - 1

Then the user will have exactly num_guess guesses, and you can keep count of how many correct guesses the user has made by setting count = 0 before the while , and incrementing ( count = count + 1 ) it each time the user guesses correctly. 然后,用户将获得确切的num_guess猜测,并且可以通过在while之前将count = 0设置count = 0 ,并在每次用户正确猜测时将其递增( count = count + 1 )来保留用户做出的正确猜测的count = 0 In this case the number of wrong guesses will surely be the total number of guesses minus the correct ones ( count ). 在这种情况下, 错误的猜测数肯定会是猜测的总数减去正确的猜测数( count )。

As an added bonus, all your if statements will be much shorter since you won't have 4 variables for 4 guesses. 另外,所有的if语句都将短得多,因为您不会有4个猜测的4个变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM