Kabdmenrs
Kabdmenrs

Reputation: 45

Python 3.6 : My variable will not add a number after each guess

My code works and everything runs fine except the counter does not add a number after each guess. It just always outputs "guess = 1" no matter how many guesses the user has.

import random

number = random.randint(1,100)
counter = 1

while True:
    pick = input("Choose a number between 1 and 100. ")
    pick = int(pick)
    if pick == number:
        print("You are correct, great job! ")
        print("\n")
        print("How many guesses you had: ", counter)
        print("\n")
        continue

    while True:
        if pick > number:
            print("That was too high of a guess. Try again. ")
            counter + 1
            print("How many guesses you have had: ", counter)
            print("\n")
            break       
        elif pick < number:
            print("That was too low of a guess. Try again. ")
            counter + 1          
            print("How many guesses you have had: ", counter)
            print("\n")
            break

Upvotes: 0

Views: 34

Answers (1)

LAS
LAS

Reputation: 819

Set the counter

import random

number = random.randint(1,100)
counter = 1

while True:
    pick = input("Choose a number between 1 and 100. ")
    pick = int(pick)
    if pick == number:
        print("You are correct, great job! ")
        print("\n")
        print("How many guesses you had: ", counter)
        print("\n")
        continue

    while True:
        if pick > number:
            print("That was too high of a guess. Try again. ")
            counter = counter + 1
            # counter +=1  # alternate form
            print("How many guesses you have had: ", counter)
            print("\n")
            break       
        elif pick < number:
            print("That was too low of a guess. Try again. ")
            counter = counter + 1          
            # counter += 1  # alternate form
            print("How many guesses you have had: ", counter)
            print("\n")
            break

Upvotes: 2

Related Questions