0

I am trying to get python to do the following

while tries:
   guess = input ("Guess no: " + 3-tries+ ": ")
   ....
   tries=tries-1

but I get the error message TypeError: Can't convert 'int' object to str implicitly. How can I successfully code this?

2 Answers 2

1

You cannot cast variable of type int to string implicitly. You have to use str() method.

For example:

>>> str(10)
'10'

So, you have to write:

input ("Guess no: " + str(3-tries) + ": ")

Documentation

Sign up to request clarification or add additional context in comments.

1 Comment

@jsmith613 if that worked for you, please, consider marking my answer as Correct (:
1

You can use python's string formatting to print an int in a string

guess = input ("Guess no: {}: ".format(3-tries))

Or, for python2.6

guess = input ("Guess no: {0}: ".format(3-tries))

2 Comments

Use input("Guess no: {0}".format(3-tries)) so that python2.6 would be happy, right?
yes, python 2.6 needs an index, python >2.7 doesn't require that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.