0

I'm learning python with a book that teaches by creating games. I have to write the code so I input a number and the computer makes educated guesses based on higher or lower input. I keep getting an error after inputting my number:

Traceback (most recent call last):
  File "C:\Users\Rayvenx\Desktop\My_programs\Number_game_pcguess.py", line 7, in 
    highlow = raw_input("\nIs it", guess, "?:")
TypeError: [raw_]input expected at most 1 arguments, got 3

Here's the code:

import random

number = raw_input("\nWhat is your number 1-100? :")

guess = random.randrange(100) + 1

highlow = raw_input("\nIs it", guess, "?:")

while guess != number:
    if highlow == "lower":
        guess = random.randrange(100) + 1  guess

        highlow = raw_input("\nIs it", guess, "?:")


print "\nHaha! I win!"

raw_input("\n\nPress enter to exit game.")
0

3 Answers 3

1
highlow = raw_input("\nIs it %s?:"%guess)

Or, using the format method (introduced in Python 2.6):

highlow = raw_input("\nIs it {0}?:".format(guess))

Or, using if Python3:

highlow = raw_input("\nIs it {}?:".format(guess))
Sign up to request clarification or add additional context in comments.

Comments

1

This line is passing 3 arguments.

highlow = raw_input("\nIs it", guess, "?:")

Format the string outside or format the string inside

mystr = "\nIs it %s ?;" % guess
highlow = raw_input(mystr)

or

highlow = raw_input("\nIs it %s ?;" % guess)

Comments

0

Your problem is that you are giving raw_input three arguments when it expects one ;)

But, seriously: your calls which look like raw_input("is it", guess, "?:") should use Python's string formatting to format the string being passed to raw_input: raw_input("is it %s?" %(guess, )).

10 Comments

I don't know what that means x_x I've written about a dozen programs. All have went well up until this one. I can't figure out how to make the computer "guess" less than or more than the previous given number.
That seems like a different problem than the one you've posted about here. If my solution (or another solution) works for you, you should upvote + accept it (or them), then ask a new question for this other question you have.
I got the same error when substituting raw_input("is it %s?" %(guess, ))
Have you change all the calls to raw_input?
Like I said, I'm new to this. Not sure what you mean. I pasted what you wrote into the code in place of how I had it written <raw_input("is it %s?" %(guess, ))> and it gave me the same error.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.