简体   繁体   中英

Python Beginner - Number Guessing Game print issue

I'm having an issue simply with print("Out of Guesses,\nYou Lose. Try Again,"): Whenever, the player runs out of guesses it prints out all print print statements twice here's the output:

Too Low!  
Out of Guesses!  
You Lose, Try Again!  
Too Low!

When it should just print:

Out of Guesses!
You Lose, Try Again!

Code:

import random

number = random.randint(0,20)
player = ""
guess_count = 0
guess_limit = 5
out_guesses = False

while player != number and not out_guesses:
    if guess_count < guess_limit:
        player = int(input("Input Your Guess: "))
        guess_count += 1

    else:
        out_guesses = True
        print("Out of Guesses!\nYou Lose, Try Again!")

    if player == number:
        print("You Win!")
    if player < number:
        print("Too Low!")
    if player > number:
        print("Too High!")

In out_guesses scenario:

  if player == number:
        print("You Win!")
    if player < number:
        print("Too Low!")
    if player > number:
        print("Too High!")

This code block is getting executed twice instead of one. You've to stop it by logic. Here is a sample:

import random

number = random.randint(0, 20)
player = ""
guess_count = 0
guess_limit = 5

while player != number:
    if guess_count < guess_limit:
        player = int(input("Input Your Guess: "))
        guess_count += 1
        if player == number:
            print("You Win!")
        if player < number:
            print("Too Low!")
        if player > number:
            print("Too High!")
    else:
        print("Out of Guesses!\nYou Lose, Try Again!")
        break

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