简体   繁体   English

我尝试在我的猜谜游戏中添加一个简单的重玩功能

[英]I tried adding a simple play again feature to my guessing game

I tried to add a do you want to play again feature to my guessing game and it stopped working :( pls help. Im very new to python so i bet i have done many oblivious mistakes :S我试图在我的猜谜游戏中添加一个你想再玩一次的功能,但它停止工作了:( 请帮忙。我对 python 非常陌生,所以我敢打赌我犯了很多不为人知的错误:S

import random

n = random.randint(1, 100)

play_game = input("Do you want to play ? Y/N :")
play_game = play_game.upper()
print("play_game")

while play_game == 'Y':
    guess = int(input("Guess a number between 1 och 100: "))
while n != "gissning":
    if guess < n:
        print("You guessed to low")
        guess = int(input("Guess a number between 1 och 100: "))
    elif guess > n:
        print ("You guessed to high")
        guess = int(input("Guess a number between 1 och 100: "))
    else:
        print("Gratz you guessed it")
        break
    
while play_game == 'Y':
    # your game 

    play_game = input("Do you want to play again? Y/N :").upper()

Actually there are a few little problems in your code, but with a bit of logic you can figure it out.其实你的代码有一些小问题,但是稍微有点逻辑你就可以搞清楚了。

First, you don't want three separated while loops, since when you quit one you'll never reach it again unless you restart your code.首先,你不想要三个分开的while循环,因为当你退出一个循环时,除非你重新启动你的代码,否则你将永远不会再次到达它。 You actually want nested loops .您实际上想要嵌套循环 The outer one will verify if the user wants to play again, while the inner one will keep asking guesses until it matches the random number.外层会验证用户是否想再次玩,而内层会不断询问猜测,直到匹配随机数。

And second, you want to compare n , the random number, to guess , the user's input.其次,您想比较n (随机数)和guess (用户输入)。 In your code you're comparing n != "gissning" , which will never be true since n is a number and "gissning" , a string.在您的代码中,您正在比较n != "gissning" ,这永远不会成立,因为n是一个数字,而"gissning"是一个字符串。

With that in mind you can change your code a little bit and have something like:考虑到这一点,您可以稍微更改代码并具有以下内容:

import random

print("play_game")
play_game = input("Do you want to play ? Y/N :").upper()
highscore = 0

while play_game == 'Y':
    n = random.randint(1, 100)
    guess = int(input("Guess a number between 1 och 100: "))
    score = 1
    while n != guess:
        if guess < n:
            print("You guessed to low")
        elif guess > n:
            print("You guessed to high")
        guess = int(input("Guess a number between 1 och 100: "))
        score += 1
    else:
        print("Gratz you guessed it")
        highscore = score if score < highscore or highscore == 0 else highscore
        print('Your score this turn was', score)
        print('Your highscore is', highscore)
        play_game = input("Do you want to play again? Y/N :").upper()

Hope this helps you out.希望这可以帮助你。 Good luck in your Python journey!祝你的 Python 之旅好运! Let us know if you have any further questions.如果您有任何其他问题,请告诉我们。

The next input prompt should be inside the outer-while loop.下一个输入提示应该在外循环中。 You also need to print the "Gratz you guessed it" along with the prompt and outside the inner-while loop when it's already n == guess.当它已经是 n == guess 时,您还需要打印“Gratz you guessed it”以及提示和内部 while 循环之外。

import random

play_game = input("Do you want to play ? Y/N :")
play_game = play_game.upper()

while play_game == 'Y':
  print("play_game")
  n = random.randint(1, 100)

  guess = int(input("Guess a number between 1 och 100: "))
  while n != guess:
    if guess < n:
        print("You guessed to low")
        guess = int(input("Guess a number between 1 och 100: "))
    elif guess > n:
        print ("You guessed to high")
        guess = int(input("Guess a number between 1 och 100: "))

  print("Gratz you guessed it")

  play_game = input("Do you want to play ? Y/N :")
  play_game = play_game.upper()
  

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

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