简体   繁体   中英

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

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. 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. In your code you're comparing n != "gissning" , which will never be true since n is a number and "gissning" , a string.

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! 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.

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()
  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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