[Tutor] unorderable types
Cameron Simpson
cs at cskk.id.au
Sun Aug 6 03:40:02 EDT 2017
On 06Aug2017 07:19, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:
>On 05/08/17 19:28, Howard Lawrence wrote:
>> if guess_value != number:
>> number = str(number)
>> print ('nope. the number i was thinking of was ' + number)
>
>There is the problem, you convert number to a str before printing
>it. so next iteration of the loop your if test fails.
>
>You don't need the conversion in this case because print does it
>automatically.
I should point out that print doesn't magicly let you "+" a str and an int. You
would need to write:
print ('nope. the number i was thinking of was ', number)
You can see that that doesn't use "+". The print function calls str() on each
of its arguments, then writes the result to the output. str() on the string
returns itself, and str(number) returns the number in text form.
You can see then that you don't need str(number) yourself, and therefore do not
need to store it in a variable.
Also, while you can bind a value of any type to any variable, as you have here
by binding an int to "number" and then later a str to "number", it is not a
great idea. A particular variable should usually always hold the same type of
value; this storing of different types of values in the same variable
contributed to your problem.
Cheers,
Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)
More information about the Tutor
mailing list