Bạn có thể làm điều đó như thế này
ch = 'y'
while[ch == 'y']:
num = 2
# Prompt the user to enter a number.
# Store the number entered by the user in the variable n.
while True:
if isinstance[num, int]:
if num50:
num = input['Try again. Your number must be between 1-50: ']
else:
num = input['Enter a whole number between 1 and 50: ']
else:
num = input['Enter a whole number between 1 and 50: ']
try:
num = int[num]
if num50:
continue
break
except ValueError:
print["You did not enter a number, try again."]
# Check if the valid number entered by the user is even or odd.
# If number mod 2 == 0, then the number is even
if num%2 == 0:
# Print the message.
print['']
print['Your number is an even number. ']
# Initialize the variable even_sum to 0.
# This variable is used to store sum of even numbers
even_sum=0
# for loop to find sum of even numbers
for i in range[2,num+1,2]:
even_sum+=i
# Print the sum of the even numbers from 2 till the number
# entered by the user.
print['The sum of the even numbers from 2 to %d is : '%num,even_sum]
else:
# If number mod 2 is not equal to 0, print the message.
# So, the number is odd.
print['']
print['Your number is an odd number.']
# Initialize the variable odd_sum to 0.
# This variable is used to store sum of odd numbers
odd_sum=0
# for loop to find sum of odd numbers
for i in range[1,num+1,2]:
odd_sum+=i
# Print the sum of the odd numbers from 1 till the number
# entered by the user.
print['The sum of odd numbers from 1 to %d is: '%num,odd_sum]
print['']
# Ask the user whether he/she wants to continue the program.
ch = input["\n Try again? [y/n] "]
Bài kiểm tra
Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? [y/n] y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? [y/n] n
Viết chương trình Python để tìm xem một số đã cho [chấp nhận từ người dùng] là chẵn hay lẻ, in ra một thông báo thích hợp cho người dùng
Trình bày bằng hình ảnh của các số chẵn
Trình bày bằng hình ảnh của các số lẻ
Giải pháp mẫu. -
Mã Python
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
Đầu ra mẫu
Enter a number: 5 This is an odd number.
Số chẵn từ 1 đến 100
Các số lẻ từ 1 đến 100
Sơ đồ
Trực quan hóa việc thực thi mã Python
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực thi chương trình nói trên
Ví dụ #3. Lấy giới hạn phạm vi từ đầu vào của người dùng hoặc với đầu vào tĩnh để giảm thời gian thực thi mã và tăng hiệu suất mã
Python3
[5, 7, 9, 11, 13, 15]4
[5, 7, 9, 11, 13, 15]5
[5, 7, 9, 11, 13, 15]6
[5, 7, 9, 11, 13, 15]7
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 115
5 7 9 11 13 15 17 193
Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? [y/n] y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? [y/n] n
00Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? [y/n] y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? [y/n] n
135 7 9 11 13 15 17 193
Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? [y/n] y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? [y/n] n
03
5 7 9 11 13 15 17 190
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 115
5 7 9 11 13 15 17 192
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 194
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 196
5 7 9 11 13 15 17 197
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
265 7 9 11 13 15 17 198
5 7 9 11 13 15 17 199
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
20 num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
21num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
22num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
23 num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
245 7 9 11 13 15 17 195
5 7 9 11 13 15 17 193
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
255 7 9 11 13 15 17 198
5 7 9 11 13 15 17 199
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 110
5 7 9 11 13 15 17 193
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 112
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 113
5 7 9 11 13 15 17 1919
5 7 9 11 13 15 17 197
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
265 7 9 11 13 15 17 198
5 7 9 11 13 15 17 199_______220
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
215 7 9 11 13 15 17 1926
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
23num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
245 7 9 11 13 15 17 1929
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
23 num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
245 7 9 11 13 15 17 195
5 7 9 11 13 15 17 193
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
255 7 9 11 13 15 17 198
5 7 9 11 13 15 17 199
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 110
5 7 9 11 13 15 17 193
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 112
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 113
Đầu ra
5 7 9 11 13 15 17 19
Độ phức tạp về thời gian. O[N], Ở đây N là sự khác biệt của kết thúc và bắt đầu.
Không gian phụ. O[1], Vì không gian thừa không đổi được sử dụng.
Ví dụ #4. Lấy giới hạn phạm vi từ đầu vào của người dùng
Python3
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 114
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 115
5 7 9 11 13 15 17 193
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 117
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 118
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 119
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 118
5 7 9 11 13 15 17 1948
Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? [y/n] y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? [y/n] n
12Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? [y/n] y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? [y/n] n
135 7 9 11 13 15 17 193
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 117
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 118
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 119
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 118
5 7 9 11 13 15 17 1956
Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? [y/n] y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? [y/n] n
12
5 7 9 11 13 15 17 1958
5 7 9 11 13 15 17 1959
5 7 9 11 13 15 17 193
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
21num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
22num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
23 num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
245 7 9 11 13 15 17 1965
5 7 9 11 13 15 17 192
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 1968
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 1970
5 7 9 11 13 15 17 198
5 7 9 11 13 15 17 199
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
20 5 7 9 11 13 15 17 1974
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
265 7 9 11 13 15 17 199
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 110
5 7 9 11 13 15 17 193
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 112
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 113________số 8
Độ phức tạp về thời gian. O[n]
Không gian phụ. O[1], Vì không gian thừa không đổi được sử dụng.
Phương pháp. Sử dụng hàm lambda
Python3
5 7 9 11 13 15 17 1981
5 7 9 11 13 15 17 1982
5 7 9 11 13 15 17 1983______93____985
5 7 9 11 13 15 17 1986
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 1988
_______989____93____991
5 7 9 11 13 15 17 198
5 7 9 11 13 15 17 1993
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
20 num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
215 7 9 11 13 15 17 1996
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
23num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
24num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
25num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
26num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
201num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
202num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2035 7 9 11 13 15 17 193
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
205_______88num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
207Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 118______2209
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2105 7 9 11 13 15 17 192
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 194
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 196
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2165 7 9 11 13 15 17 199
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
218Đầu ra
Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? [y/n] y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? [y/n] n
1Độ phức tạp về thời gian. O[n]
Không gian phụ. O[1], Vì không gian thừa không đổi được sử dụng.
Phương pháp. Sử dụng đệ quy
Python3
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
219 num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
220num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
265 7 9 11 13 15 17 190
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2235 7 9 11 13 15 17 198
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
225num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
265 7 9 11 13 15 17 199
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
228num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
23num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
24num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2315 7 9 11 13 15 17 193
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 112
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 113
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
26num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
225 num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
237____2235 7 9 11 13 15 17 193
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
240num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2415 7 9 11 13 15 17 193______94
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2445 7 9 11 13 15 17 193
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
246num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
247Đầu ra
Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? [y/n] y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? [y/n] n
0Độ phức tạp về thời gian. O[n]
Không gian phụ. O[1], Vì hàm là đệ quy đuôi nên không sử dụng không gian ngăn xếp.
Phương pháp. Sử dụng hiểu danh sách
Python3
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2485 7 9 11 13 15 17 193
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
250_______98 5 7 9 11 13 15 17 1993
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
20 num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
21______885 7 9 11 13 15 17 194
5 7 9 11 13 15 17 195
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
246num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
23num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
24Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 113
5 7 9 11 13 15 17 190
5 7 9 11 13 15 17 1993
5 7 9 11 13 15 17 192
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 194
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 196
5 7 9 11 13 15 17 1970
5 7 9 11 13 15 17 199
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 118____2272
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
273Đầu ra
Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? [y/n] y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? [y/n] n
98Độ phức tạp về thời gian. O[n]
Không gian phụ. O[1], Vì không gian thừa không đổi được sử dụng.
Phương pháp. Sử dụng hàm liệt kê
Python3
5 7 9 11 13 15 17 1983______93
5 7 9 11 13 15 17 194
5 7 9 11 13 15 17 1986
5 7 9 11 13 15 17 193
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
246num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2805 7 9 11 13 15 17 193
5 7 9 11 13 15 17 1991
5 7 9 11 13 15 17 198
5 7 9 11 13 15 17 1993
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
20 num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
215 7 9 11 13 15 17 1996
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
23num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
24num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
25num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
291____22925 7 9 11 13 15 17 199
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2945 7 9 11 13 15 17 198
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
296num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
20 num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
298num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2995 7 9 11 13 15 17 190
5 7 9 11 13 15 17 1983
5 7 9 11 13 15 17 192
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 194
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 196
5 7 9 11 13 15 17 1907
Đầu ra
[5, 7, 9, 11, 13, 15]
Độ phức tạp về thời gian. O[n]
Không gian phụ. O[1], Vì không gian thừa không đổi được sử dụng.
Phương pháp. Sử dụng thẻ
Python3
5 7 9 11 13 15 17 1983______93____94
5 7 9 11 13 15 17 1986
5 7 9 11 13 15 17 193
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2465 7 9 11 13 15 17 198
5 7 9 11 13 15 17 1993
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
20 num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
215 7 9 11 13 15 17 1996
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
23num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
24num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
25num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2915 7 9 11 13 15 17 190
5 7 9 11 13 15 17 1993
5 7 9 11 13 15 17 192
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 196
5 7 9 11 13 15 17 197
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
265 7 9 11 13 15 17 1932
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
2915 7 9 11 13 15 17 1919
5 7 9 11 13 15 17 197
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
265 7 9 11 13 15 17 199
5 7 9 11 13 15 17 1938
5 7 9 11 13 15 17 193
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 112
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 113
Đầu ra
Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? [y/n] y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? [y/n] n
0Độ phức tạp về thời gian. O[n]
Không gian phụ. O[1], Vì không gian thừa không đổi được sử dụng.
Phương pháp. Sử dụng phương pháp lọc
Python3
Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 114
5 7 9 11 13 15 17 1943
[5, 7, 9, 11, 13, 15]7
5 7 9 11 13 15 17 1983____93____94
5 7 9 11 13 15 17 1948
5 7 9 11 13 15 17 1949
5 7 9 11 13 15 17 193____2246
5 7 9 11 13 15 17 1948
5 7 9 11 13 15 17 1958
5 7 9 11 13 15 17 1954
5 7 9 11 13 15 17 193
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
207_______88num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
209 5 7 9 11 13 15 17 1959
5 7 9 11 13 15 17 192
5 7 9 11 13 15 17 193
5 7 9 11 13 15 17 195
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
215 7 9 11 13 15 17 1964
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
23num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
24Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? [y/n] y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? [y/n] n
12