Matt
Matt

Reputation: 3

Problem with creating variable in loop

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.")

Upvotes: 0

Views: 1016

Answers (3)

JiminyCricket
JiminyCricket

Reputation: 7410

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)

Upvotes: 1

unutbu
unutbu

Reputation: 879701

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))

Upvotes: 1

David Wolever
David Wolever

Reputation: 154504

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, )).

Upvotes: 0

Related Questions