数が奇数であるかどうかわかる3つの方法
液角1
def find(num):
# code logic here
if num%2 == 0:
numtype="even"
else:
numtype = "odd"
return numtype
num = int(input('Enter the number: ')) # 1. take your input
numtype = find(num) # 2. call the find function
print('Given number is',numtype). # 3. print if the number is even or odd
出力:
coder# python challenge07.py
Enter the number: 5
Given number is odd
coder# python challenge07.py
Enter the number: 8
Given number is even
解説:
液角2
NumTypeへのデフォルト値(ODD)を割り当てることによる他のブロックの使用を避けてください
def find(num):
# code logic here
numtype = "odd"
if num%2 == 0:
numtype="even"
return numtype
num = int(input('Enter the number: ')) # take your input
numtype = find(num) # call the find function
print('Given number is',numtype) # print if the number is even or odd
出力:
coder# python challenge07.py
Enter the number: 5
Given number is odd
coder# python challenge07.py
Enter the number: 8
Given number is even
1対1
直接リターンnumtype
def find(num):
# code logic here
if num%2 == 0:
return "even"
return "odd"
num = int(input('Enter the number: ')) # take your input
numtype = find(num) # call the find function
print('Given number is',numtype) # print if the number is even or odd
出力:
coder# python challenge07.py
Enter the number: 5
Given number is odd
coder# python challenge07.py
Enter the number: 8
Given number is even
ボーナス
numtype = num%2 == 0 and "even" or "odd"
ビデオ説明もReference
この問題について(数が奇数であるかどうかわかる3つの方法), 我々は、より多くの情報をここで見つけました https://dev.to/vikkyomkar/3-ways-to-find-if-a-number-is-odd-even-in-python-1ao7テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol