2

I have created a Python program that guesses the number programmer thinks in mind. Everything is working file but i don't know how to use guess in print statement in a way that print statement display number as well. I tried adding word "guess" but it is not working. I am C programmer and working with Python for the first time, so i am unable to figure it out.

hi = 100 lo = 0 guessed = False print ("Please think of a number between 0 and 100!") while not guessed: guess = (hi + lo)/2 print ("Is your secret number " + //Here i need to Display the guessed Number + "?") user_inp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too" "low. Enter 'c' to indicate I guessed correctly: ") if user_inp == 'c': guessed = True elif user_inp == 'h': hi = guess elif user_inp == 'l': lo = guess else: print ("Sorry, I did not understand your input.") print ("Game over. Your secret number was: " + //Here i need to display final number saved in guess.) 

3 Answers 3

3

Just convert it to string.

print ("Game over. Your secret number was: " + str(guess)) 

You could also use string formatting.

print("Game over. Your secret number was {}".format(guess)) 

Or, since you come from a C background, old style string formatting.

print("Game over. Your secret number was %d." % guess) 
Sign up to request clarification or add additional context in comments.

Comments

1

Try this in your print statement and let me know if it works.

str(guess) 

Comments

1

Python has very nice string formatting, which comes in handy when you need to insert multiple variables:

message = "Game over after {n} guesses. Your number was {g} (pooh... got it in {n} times)".format(g=guess, n=number) print(message) 

Comments