I'm new to Python. I have a question about "War Game" in the book "Self-study Programmer".
# War Game code
There were the following functions in the code.
def __lt __ (self, c2): if self.value<c2.value: return True if self.value == c2.value: if self.suit<c2.suit: return True else: return False return False
I don't understand the role of return False on the last line. I tried to execute the code without the last line in the experiment, but it worked without any problems.
I would appreciate your reply.
Answer # 1
Wouldn't it be convinced if you write as follows?
def __lt __ (self, c2): if self.value<c2.value: return True elif self.value == c2.value: if self.suit<c2.suit: return True else: return False else: return False
If you return the result, you exit the function, so you can omit the else clause.
I tried to run the code without the last line in the experiment, but it worked fine.
As already answered, None is returned, which can be determined as a false value.
return Falseshould not be omitted semantically.
Answer # 2
If there is no return in python, None will be returned, but it will be False at the time of evaluation, so the result will be the same.
Answer # 3
In Python, the value
Noneis returned without writing
Falseare both false as truth values (reference), the result thrown into
ifetc. will not change .
Of course, if the processing is different depending on whether it is
False, the result will change.
- python - about importing intertools
- python - about the list when calling a function in another file
- python - about numpy's order argument
- python 3x - about 2's complement representation
- python - about the value acquired by for sentence
- about python timeout
- python - about pandas permissionerror: [winerror 32]
- about python flask
- about line break display in python
- about installing python-twitter ・i would like to know the difference between tweepy and python-twitter
- tell me about python iteration statements
- about installing python
- python: about list arguments
- python - [django] about access from another terminal in lan
- python - about keras maxpooling2d
- about basic syntax of python
- python - about a problem of satcoder agc39
- [python] about atcoder arc006b
- python - about the error "allocation of 536870912 exceeds 10% of system memory" when learning cnn
- [python] about aoj-0105 book index
- python 3x - typeerror: 'method' object is not subscriptable
- python - you may need to restart the kernel to use updated packages error
- xcode - pod install [!] no `podfile 'found in the project directory
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture
- android studio - unresolved reference comes out in kotlin
- mysql startup failed [error] innodb: the innodb_system data file 'ibdata1' must be writable
- django - oserror: [winerror 123] the file name, directory name, or volume label syntax is incorrect : '<frozen importlib_boot
- python - importerror: cannot import name md5 error cannot be resolved