Solusi #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
Keluaran
coder# python challenge07.py
Enter the number: 5
Given number is odd
coder# python challenge07.py
Enter the number: 8
Given number is even
_Penjelasan. -
- fungsi input[] digunakan untuk mengambil input pengguna
- fungsi find[] dipanggil untuk memeriksa apakah suatu angka mati/genap. Fungsi ini mengembalikan numtype sebagai ganjil/genap
- Terakhir, cetak jika angka yang diberikan ganjil/genap
Solusi #2
Hindari penggunaan blok else dengan menetapkan nilai default [ganjil] ke numtype
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
Keluaran
coder# python challenge07.py
Enter the number: 5
Given number is odd
coder# python challenge07.py
Enter the number: 8
Given number is even
_Solusi #3
langsung mengembalikan 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
_Keluaran
coder# python challenge07.py
Enter the number: 5
Given number is odd
coder# python challenge07.py
Enter the number: 8
Given number is even
_Bonus
numtype = num%2 == 0 and "even" or "odd"
Temukan juga penjelasan videonya
Pada artikel ini, Kita akan melihat bagaimana menulis program untuk mencari bilangan Genap atau Ganjil Menggunakan Metode Rekursi. Jika nomornya dikembalikan benar, yang lain salah dengan Python
Untuk itu, kami menggunakan Operator yang disebut Modulus Operator. Operator ini digunakan dalam operasi saat kita perlu menghitung sisa angka tersebut saat dibagi dengan pembagi mana pun
- Angka Genap. Bilangan yang habis dibagi 2 maka sisa nya adalah 0
- Angka ganjil. Bilangan yang tidak habis dibagi 2 maka sisa nya adalah 1
Contoh
Input: 2 Output: True Explanation : 2 % 2==0 So True Input: 5 Output: False Explanation : 2 % 2 != 0 So, False_
Metode #1
Mendekati
- Kita menggunakan konsep mendapatkan sisa tanpa menggunakan operator modulus dengan cara mengurangkan bilangan dengan bilangan ke-2
- Jika akhirnya, kita mendapatkan sisa maka angka itu ganjil dan kembalikan False untuk angka itu
- Kalau tidak, angkanya genap dan kembalikan True untuk angka itu
Python3
# defining the function having the one parameter as input
def
evenOdd[n]:
3 num is odd0
3 num is odd2
3 num is odd3
3 num is odd4
3 num is odd4
3 num is odd6
3 num is odd7
3 num is odd8
3 num is odd9
3 num is odd0
3 num is odd1
3 num is odd3
3 num is odd5
3 num is odd3
3 num is odd4
3 num is odd4
3 num is odd9
3 num is odd7
3 num is odd8
3 num is odd9
3 num is odd3
3 num is odd5
3 num is odd6
________7______8
3 num is odd9
3 num is odd9
2 4 6 8 100
2 4 6 8 101
2 4 6 8 102
3 num is odd1
2 4 6 8 10_4
2 4 6 8 105
3 num is odd4
2 4 6 8 107
3 num is odd2
2 4 6 8 109
# defining the function having the one parameter as input
1# defining the function having the one parameter as input
2# defining the function having the one parameter as input
3
2 4 6 8 102
3 num is odd5
3 num is odd6
# defining the function having the one parameter as input
1# defining the function having the one parameter as input
2def
0
2 4 6 8 102
Keluaran
3 num is odd
Metode #2
Kami menggunakan operator modulus untuk menemukan genap atau ganjil Seperti jika num % 2 == 0 maka sisanya adalah 0 sehingga angka yang diberikan adalah genap dan mengembalikan True
Lain jika num % 2. = 0 maka sisanya bukan nol sehingga angka yang diberikan adalah ganjil dan kembali Salah
Python3
def
_2
def
_3
def
evenOdd[n]:
3 num is odd0
3 num is odd2
3 num is odd3
evenOdd[n]:
2 2 4 6 8 101
3 num is odd4
3 num is odd4
3 num is odd6
3 num is odd7
3 num is odd8
3 num is odd9
3 num is odd0
3 num is odd3
3 num is odd5
3 num is odd3
evenOdd[n]:
22 4 6 8 101
93 num is odd4
3 num is odd6
3 num is odd7
3 num is odd8
3 num is odd9
3 num is odd3
3 num is odd5
3 num is odd6
3 num is odd8
3 num is odd9
3 num is odd01
2 4 6 8 10_4
2 4 6 8 105
3 num is odd4
2 4 6 8 107
3 num is odd2
3 num is odd07
# defining the function having the one parameter as input
1
3 num is odd10
# defining the function having the one parameter as input
32 4 6 8 102
3 num is odd5
3 num is odd6
# defining the function having the one parameter as input
1
3 num is odd10
def
02 4 6 8 102
Keluaran
3 num is odd
Metode #3. Menggunakan operator BINARY AND [&].
Mendekati. Untuk metode ini, kita hanya perlu menggunakan operator &, yang merupakan operator bitwise, dan cara kerjanya sama seperti operator % untuk kasus khusus ini.