Home > OS >  Working on a python problem trying to guess a randomly generated number im generally new to python a
Working on a python problem trying to guess a randomly generated number im generally new to python a

Time:10-26

having trouble when it trys comparing the input and the random integer

import random
target = random.randint(1,100)
counter = 0
print(target)
print(int(input('Guess an integer between 1 and 100 ')))
guess = input
while guess != target():
    if guess > target():
        print("Guess is to high")
    elif guess < target():
        print("Guess was to low")
    counter = counter   1
    print(int(input('Guess an integer between 1 and 100 ')))
    
print("Correct it took you "   str(counter)   " guesses to get the correct number")

i tried changing the variable names starting from scratch several times googled tons of stuff but im lost and and confused and need an answer so please if you understand my problem id appreciate some advice

CodePudding user response:

Problems:

  1. print(int(input('Guess an integer between 1 and 100 '))) is reading a value, converting it to int, printing it, then throwing it away so your guesses are never used
  2. guess = input is assigning the function input to guess (which is useless, and actively harmful when you get to comparing to target, since a function and an int are not orderable relative to one another); since you never reassign it, this never changes
  3. target should never be called (replace every use of target() with target), it's an int, not a callable function
  4. Minor: If possible, it's nicer to avoid duplicating code; the Python 3.8 walrus operator (:=) lets you both read a value and compare in the while conditional, which avoids the need to double the input code

Fixing all of these gets:

import random
target = random.randint(1,100)
print(target)
counter = 1  # Start counter at 1, since we can't increment it if the input actually matches
# Walrus allows reading, assigning, and testing in a single line
while (guess := int(input('Guess an integer between 1 and 100 '))) != target:
    if guess > target:
        print("Guess is too high")  # sp fix
    elif guess < target:
        print("Guess was too low")  # sp fix
    counter  = 1  # Save a little typing by using counter  = 1 instead of counter = counter   1

 # f-string saves manual string conversion and piecemeal concatenation
 print(f"Correct it took you {counter} guesses to get the correct number")

CodePudding user response:

You have several issues with your code:

  1. You have print(int(input('Guess an integer between 1 and 100 '))) followed by guess = input. You are not assigning to guess what was entered but rather setting it to refer to a function.
  2. In several places you have target(). But target is of type int, not a function, and you cannot call it as if it were a function.
  3. After a user enters a guess, it narrow downs the range of legal guess and therefore your prompt should change accordingly.
  4. You should check for guess outside the legal range.
import random

target = random.randint(1,100)
low = 1
high = 100

counter = 0
while True:
    guess = int(input(f'Guess an integer between {low} and {high}: '))
    if guess == target:
        break
    if not low <= guess <= high:
        print('Your guess is outside the legal range of guesses.')
    elif guess > target:
        print("Guess is to high.")
        high = guess - 1
    else:
        print("Guess was to low.")
        low = guess   1
    counter = counter   1

print("Correct! It took you", counter, "guesses to get the correct number.")

CodePudding user response:

I have added new changes, get rid of # while guess != target

import random
target = random.randint(1,100)
counter = 0
print(target)
check=False
while not check:
   guess = int(input('Guess an integer between 1 and 100 '))

   if guess > target:
    print("Guess is to high")
   elif guess < target:
    print("Guess was to low")
   else:
    check = True
    break
   counter = counter   1
  
  
print("Correct it took you "   str(counter)   " guesses to get the correct number")enter code here

CodePudding user response:

Try these changes,

import random
target = random.randint(1,100)
counter = 0
print(target)
guess = int(input('Guess an integer between 1 and 100 '))
while guess != target:
   if guess > target:
     print("Guess is to high")
   elif guess < target:
     print("Guess was to low")
counter = counter   1


print("Correct it took you "   str(counter)   " guesses to get the correct number")

CodePudding user response:

You do not to print when you ask for an input value, as that function will take care of printing the text you give it. You're also not updating guess after a new request. Try this instead:

import random                                                                                           
target = random.randint(1,100)                                                                          
                                                                                                        
counter = 0                                                                                             
print(target)                                                                                           
                                                                                                        
guess = int(input('Guess an integer between 1 and 100 '))                                               
while guess != target:                                                                                  
    if guess > target:                                                                                  
        print("Guess is to high")                                                                       
    elif guess < target:                                                                                
        print("Guess was to low")                                                                       
    counter = counter   1                                                                               
    guess = int(input('Guess an integer between 1 and 100 '))                                           
                                                                                                        
print("Correct it took you "   str(counter)   " guesses to get the correct number")
  • Related