A memo of O'Reilly Japan's "Introduction to Mathematics Starting with Python" exercise.
It is just a personal study memo.
def print_num(number):
# print([number + 2 for i in range(2, 11) if number % 2 == 0])
print('Is an even number' if number % 2 == 0 else 'Odd')
for item in range(2, 11):
number += 2
print(int(number), end=" ")
if __name__ == '__main__':
number = float(input('what your number: '))
if number.is_integer():
print_num(number)
else:
print('Please enter an integer.')
Improvement points It is good when an integer or decimal number is entered, but it ends abnormally when an alphabet or the like is entered. So I put try..except.
def print_num(number):
print('Is an even number' if number % 2 == 0 else 'Odd')
for item in range(2, 11):
number += 2
print(int(number), end=" ")
if __name__ == '__main__':
try:
number = float(input('what your number: '))
if number.is_integer():
print_num(number)
else:
print('Please enter an integer.')
except ValueError:
print('Please enter an integer.')
Improvements were made with reference to the answers. There are some differences from the answer, such as how to write an if statement, but this is OK. In the future, for example, I would like to verify what to do from the perspective of performance.
Recommended Posts