数が奇数であるかどうかわかる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

解説:
  • input ()関数はユーザ入力
  • を使用します.
  • find ()関数は、数値がオフになっているかどうかをチェックするために呼び出されます.この関数は、奇数/偶数
  • としてnumtypeを返します.
  • 最後に、指定された数が奇数であるならば、印刷してください

    液角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"ビデオ説明も