Jul-31-2018, 06:39 PM
cmd_array[] contains ['EVENT_EXTANT_WINDOW', '82', '1', '10', '0']
bool_expected = bool(cmd_array[4])
returns True
Why?
Thanks
bool_expected = bool(cmd_array[4])
returns True
Why?
Thanks
bool
|
Jul-31-2018, 06:39 PM
cmd_array[] contains ['EVENT_EXTANT_WINDOW', '82', '1', '10', '0']
bool_expected = bool(cmd_array[4]) returns True Why? Thanks
Jul-31-2018, 06:56 PM
The general idea of bool is that if it's empty, it's False. 0, the number, is empty, so it's False. '0', the string, is not empty: it has one character. So it's True.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures
Jul-31-2018, 07:08 PM
Thanks for posting.
So the numbers in the list are strings or an object? Should I cast it to int first? bool_expected = bool(int(cmd_array[4]))
In Python, everything is an object and almost any container can store objects of different types/classes. Even functions and custom class instances.
For example: >>> def double(num): ... return num + num ... >>> a_list = ['This is a string object', True, None, 0, -14.5, 42, 0 , double, '11'] >>> for element in a_list: ... print(type(element)) ... <class 'str'> <class 'bool'> <class 'NoneType'> <class 'int'> <class 'float'> <class 'int'> <class 'int'> <class 'function'> <class 'str'>So the first element is a string obviously as the last one. There is a None, a boolean type, some integers, a float and a function which you can point to and call it from the list: >>> a_list[-2](8) 16
Jul-31-2018, 08:54 PM
Everything in your list is a string. So if you want the zeros in the list to be False, you either need to convert them to ints, or you need to explicitly test for the string '0' (
cmd_array[4] != '0' ).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures
Aug-01-2018, 12:01 AM
I wanted to convert the string numbers to 1 or zero but this did not work, it still gave me True.
expected = bool(int(cmd_array[4])) It looks like bool reports whether or not it exists and not zeroness. So I switched to: expected = True if int(cmd_array[4]) > 0 else False and that works and thus my whole test function now works. So hopefully I will now go back to other languages and leave you folks alone. Thanks.
Aug-01-2018, 03:09 AM
try
bool('0'),bool(0) and see what you get.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American. |
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
Bool Object is not Subscriptable | quest | 1 | 4,233 |
May-02-2021, 11:12 AM Last Post: Yoriz |
|
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() | Rejoice | 6 | 3,377 |
Aug-25-2020, 03:50 PM Last Post: Rejoice |
|
bool b = (num == 100) this doesn't work? | MelonMusk | 2 | 2,037 |
Jun-12-2020, 02:18 AM Last Post: bowlofred |
|
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() | bongielondy | 2 | 14,239 |
Nov-27-2019, 03:25 PM Last Post: ThomasL |
|
[split] Set bool variable | RedSkeleton007 | 1 | 2,774 |
Oct-22-2017, 09:28 PM Last Post: metulburr |