-1

This is my code, the guessing part works, but not the part that should print the guesses left I'm a beginner at programming

from random import randint
random_number = randint(1, 10)
guesses_left = 3
while guesses_left > 0:
    print "you have %s guesses left" %s (guesses_left)
    guess = int(raw_input("Your guess: "))
    guesses_left -= 1
    if guess == random_number:
        print "You win!"
        break
    else:
        print"You lose.."

the print "you have %s guesses left" %s (guesses_left) is my problem

please help

I've looked at other similar problems still leaving me clueless

The error is:

 Traceback (most recent call last):
   File "python", line 5, in <module>
 NameError: name 's' is not defined
Elisha
  • 4,811
  • 4
  • 30
  • 46
  • why do you think it's a problem? there is an error? if so, what is the error message? – Elisha Dec 07 '14 at 13:00
  • sorry forgot to add the error – William Paulsson Dec 07 '14 at 13:01
  • You may consider the newer style formatting, using `{}`. See for an example http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format . Here, that would result in `print "You have {} guesses left".format(guesses_left)`. –  Dec 07 '14 at 13:02
  • now it's clear, you have an extra 's' – Elisha Dec 07 '14 at 13:02
  • 1
    In addition, since `guesses_left` is an integer, use the integer formatting: `%d` or `{:d}`. –  Dec 07 '14 at 13:04

2 Answers2

2

Change the following:

print "you have %s guesses left" % guesses_left
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
0

Change the line raising the error to:

print "you have %s guesses left" % str(guesses_left)

HarryCBurn
  • 755
  • 1
  • 8
  • 17
hariK
  • 2,722
  • 13
  • 18