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(开发者_StackOverflow中文版"\nIs it", guess, "?:") print "\nHaha! I win!" raw_input("\n\nPress enter to exit game.")
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))
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)
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, ))
.
精彩评论