要求用户再次播放

时间:2016-06-06 12:48:49

标签: python

我不知道如何再次播放游戏"循环"用我当前的代码。我把boolAgain =""所以它没有用,但我不知道从那里去哪里以及我将如何到达那里或者我是否正确地做到这一点。

#This imports the random generator
import random

#Sets up variables
strName = ""
strError1 = "ERROR! Please put in correct input."
CONLOW = 1
intTotalScore = 0
intGamesPlayed = 0
intCount = 0
intHigh = 0
intScore = 0
intRandom = 0
intGuess = 0
intGuesses = 0
boolAgain = "yes"

#This is an input asking the users name with an added error catch
while strName.isspace() or len(strName) <=1\
      or strName.isdigit() or (any(char.isdigit() for char in strName)): 
    strName = str(input("What is your name? "))

#This is to keep the game on
while boolAgain == "yes":
    #This is for inputting a level
    while True:
        intLevel = int(input("You are here to guess the random number, \n"
                         "What level do you want to play?\n"
                         "1 and 10 or 1 and 100.\n"
                         "Please answer\n\n"
                         "'1' for 1 to 10\n"
                         "'2' for 1 to 100\n"
                         "'3' for 1 to 1000\n"
                         "\n"
                         "What is your choice? "))
        if intLevel == 1:
            intHigh = 10
            intScore = 10
            intTotalScore += intScore
            intRandom = random.randint (CONLOW, intHigh)
            intGamesPlayed += 1
            print(intRandom)
            break
        elif intLevel == 2:
            intHigh = 100
            intScore = 20
            intTotalScore += intScore
            intRandom = random.randint (CONLOW, intHigh)
            intGamesPlayed += 1
            print(intRandom)
            break
        elif intLevel == 3:
            intHigh = 1000
            intScore = 30
            intTotalScore += intScore
            intRandom = random.randint (CONLOW, intHigh)
            intGamesPlayed += 1
            print(intRandom)
            break
        else:
            print(strError1)
            continue
    #used to generate the random number        


    while intGuess != intRandom:
        intGuess = int(input("Guess the number "))
        if intGuess < intRandom:
            print('Your guess is too low.') 
        elif intGuess > intRandom:
            print('Your guess is too high.')
        elif intGuess == intRandom:
            print("\n", strName, "you are right")
            print("    ", str(intGamesPlayed), "is how much you've played \n")
            print("    ", str(intScore), "is your score for this round \n")
            print("    ", str(intTotalScore), "is your total for all rounds \n")
            boolAgain = ""
        else:
            print(strError)

我的初始代码是:

> while boolAgain != "no" or boolAgain != "yes":
>     boolAgain = input("Do you want to play again? 'yes' or 'no' ")
> if boolAgain == "no":
>     print("Goodbye " + strName)
> elif boolAgain == "yes":
>     True #I really do not know what im doing here
> else:
>     print(strError1)

我发誓这是错的。请帮忙: - )

更新

我一再得到同样的负面结果。我认为解决方案可能会使缩进更好,我不是很确定。每当我输入&#34;是&#34;时,它会说&#34;你正在再次玩,&#34; + strName但它会停止并且它不会再次重复代码。

1 个答案:

答案 0 :(得分:4)

您需要一个较大的外部while循环来重新玩游戏,其中您有另一个while循环来检查yesno,以及实际的比赛。所以:

boolAgain = True
while boolAgain:
  #here is where the actual game goes
  #yay game
  #end of game

  #asking the user if he/she wants to continue:
  noAnswer = True
  while noAnswer:
    answer = input("Do you want to play again? 'yes' or 'no' ")
    if answer == 'yes':
      noAnswer = False
      print("You are playing again, " + strName)
      #boolAgain doesn't change and is still True
    if answer == 'no':
      noAnswer = False
      boolAgain = False
      print("Goodbye " + strName)
    else:
      print("you must answer 'yes' or 'no'")

请注意,布尔值为TrueFalse,而'yes''no'只是一些文字。所以True可以在if语句中轻松使用。对于“是”,您需要将其与实际字符串值'yes'进行比较。

<强>更新 另外,正如R Nar所说,将游戏置于这样的功能中:

def play_game():
  something = 1
  something_else = input("give a number higher than 1")
  if int(something_else) > something: #the number given is still text and not an integer
    result = 'win'
  else:
    result = 'lose'
  print('you ' + result + '!')

boolAgain = True
while boolAgain:
  play_game()

  #asking the user if he/she wants to continue (see code earlier in answer)

新更新:

在您的代码中,在询问姓名后,请以while boolAgain == 'yes':开头。在代码的这一点上,boolAgain还不存在。另外,将boolean boolAgain设置为True的本地python值(它是一位),而不是'yes'的文本值。所以该行应如下所示:

boolAgain = True
while boolAgain == True:

或略微缩写(技术:boolAgain == TrueTrue,与[{1}}已经相同):

boolAgain

接下来,在您的游戏中,您在boolAgain = True while boolAgain: 之后立即更改boolAgain,不要这样做。

此外,你问你的用户他们是否想要在游戏中玩游戏时再次玩游戏。你应该在while循环中问这个问题,并且回答'no'会让你退出游戏时循环。这是因为没有缩进。

在询问用户是否想再次播放时,您可以复制本文前面的print(" ", int(intTotalScore), "is your total for all rounds \n")部分并从那里开始工作