I tried this code for a "guess a number" game:
import random
print("Pick any number between 1-100 and I'll try to guess it")
x = 1
y = 100
tries = 0
answer = "whatever"
while answer != "yes":
guess = random.randint(x, y)
answer = input("Is your number ", guess, "? Or is it 'lo'wer or 'hi'gher?")
if answer == "hi":
x = guess + 1
if answer == "lo":
y = guess - 1
tries += 1
print ("Got it! Your number is ", los, "! It took me ", tries, " Tries! :)")
input("End")
But I get an error that says:
Traceback (most recent call last):
File "/home/Documents/python/numbers.py", line 11, in <module>
answer = input("Is your number ", guess, "? Or is it 'lo'wer or 'hi'gher?")
TypeError: input expected at most 1 arguments, got 3
I know that I shouldn't expect input
to take more than 1 argument, but how do I fix the problem?
input
expects a single string as mentioned in the docs
If the prompt argument is present, it is written to standard output without a trailing newline.
Use format
instead
answer= input("Is your number {}? Or is it 'lo'wer or 'hi'gher?".format(guess))