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:
print(int(input('Guess an integer between 1 and 100 ')))
is reading a value, converting it toint
, printing it, then throwing it away so your guesses are never usedguess = input
is assigning the functioninput
toguess
(which is useless, and actively harmful when you get to comparing totarget
, since a function and anint
are not orderable relative to one another); since you never reassign it, this never changestarget
should never be called (replace every use oftarget()
withtarget
), it's anint
, not a callable function- 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 thewhile
conditional, which avoids the need to double theinput
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:
- You have
print(int(input('Guess an integer between 1 and 100 ')))
followed byguess = input
. You are not assigning toguess
what was entered but rather setting it to refer to a function. - In several places you have
target()
. But target is of typeint
, not a function, and you cannot call it as if it were a function. - After a user enters a guess, it narrow downs the range of legal guess and therefore your prompt should change accordingly.
- 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")