[Tutor] Tutor Digest, Vol 162, Issue 19
Howard Lawrence
1019shaun at gmail.com
Sun Aug 6 15:41:43 EDT 2017
On Aug 6, 2017 11:00 AM, <tutor-request at python.org> wrote:
Send Tutor mailing list submissions to
tutor at python.org
To subscribe or unsubscribe via the World Wide Web, visit
https://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
tutor-request at python.org
You can reach the person managing the list at
tutor-owner at python.org
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."
Today's Topics:
1. Re: unorderable types (Cameron Simpson)
---------- Forwarded message ----------
From: Cameron Simpson <cs at cskk.id.au>
To: tutor at python.org
Cc:
Bcc:
Date: Sun, 6 Aug 2017 17:40:02 +1000
Subject: Re: [Tutor] unorderable types
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)
Thanks for everything,it's running
saw where storing two different types in the same variable!
Digesting your pointers !
_______________________________________________
Tutor maillist - Tutor at python.org
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list