Wap để hiển thị 1 50 không và hiển thị chẵn và lẻ giữa chúng trong python

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 11 
5
5 7 9 11 13 15 17 19 
3
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
00

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
13
5 7 9 11 13 15 17 19 
3
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 19 
0
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
5
5 7 9 11 13 15 17 19 
2
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
6
5 7 9 11 13 15 17 19 
7

 

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."]	
26
5 7 9 11 13 15 17 19 
8
5 7 9 11 13 15 17 19 
9
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
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."]	
22
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."]	
24
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
3
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."]	
25

5 7 9 11 13 15 17 19 
8
5 7 9 11 13 15 17 19 
9
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
0
5 7 9 11 13 15 17 19 
3
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
2
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
3

5 7 9 11 13 15 17 19 
19
5 7 9 11 13 15 17 19 
7

 

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."]	
26
5 7 9 11 13 15 17 19 
8
5 7 9 11 13 15 17 19 
9_______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."]	
21
5 7 9 11 13 15 17 19 
26
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."]	
24
5 7 9 11 13 15 17 19 
29
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."]	
24
5 7 9 11 13 15 17 19 
5
5 7 9 11 13 15 17 19 
3
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."]	
25

5 7 9 11 13 15 17 19 
8
5 7 9 11 13 15 17 19 
9
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
0
5 7 9 11 13 15 17 19 
3
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
2
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
3

Đầ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 11 
4

 

Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
5
5 7 9 11 13 15 17 19 
3
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
7
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
8
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
9
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
8
5 7 9 11 13 15 17 19 
48
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

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
13
5 7 9 11 13 15 17 19 
3
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
7
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
8
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
9
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
8
5 7 9 11 13 15 17 19 
56
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 19 
58

5 7 9 11 13 15 17 19 
59
5 7 9 11 13 15 17 19 
3
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
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."]	
22
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."]	
24
5 7 9 11 13 15 17 19 
65
5 7 9 11 13 15 17 19 
2
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
68
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
70

 

5 7 9 11 13 15 17 19 
8
5 7 9 11 13 15 17 19 
9
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 19 
74

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."]	
26
5 7 9 11 13 15 17 19 
9
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
0
5 7 9 11 13 15 17 19 
3
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
2
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
3

________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 19 
81

5 7 9 11 13 15 17 19 
82

5 7 9 11 13 15 17 19 
83______93____985
5 7 9 11 13 15 17 19 
86
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
88

_______989____93____991

5 7 9 11 13 15 17 19 
8
5 7 9 11 13 15 17 19 
93
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
5 7 9 11 13 15 17 19 
96
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."]	
24
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."]	
25

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."]	
26
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."]	
201

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."]	
202

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."]	
203
5 7 9 11 13 15 17 19 
3
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_______88
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
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
8______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."]	
210
5 7 9 11 13 15 17 19 
2
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
6
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."]	
216

5 7 9 11 13 15 17 19 
9
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."]	
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."]	
26
5 7 9 11 13 15 17 19 
0
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."]	
223

5 7 9 11 13 15 17 19 
8
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."]	
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."]	
26
5 7 9 11 13 15 17 19 
9
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."]	
228
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."]	
24
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."]	
231
5 7 9 11 13 15 17 19 
3
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
2
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
3

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."]	
26
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."]	
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____223
5 7 9 11 13 15 17 19 
3
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."]	
240

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."]	
241
5 7 9 11 13 15 17 19 
3______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."]	
244
5 7 9 11 13 15 17 19 
3
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."]	
246

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."]	
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."]	
248
5 7 9 11 13 15 17 19 
3
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 19 
93
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______88
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
5
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."]	
246
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."]	
24
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
3
5 7 9 11 13 15 17 19 
0
5 7 9 11 13 15 17 19 
93
5 7 9 11 13 15 17 19 
2
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
6
5 7 9 11 13 15 17 19 
70

5 7 9 11 13 15 17 19 
9
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
8____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 19 
83______93
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
86
5 7 9 11 13 15 17 19 
3
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."]	
246
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."]	
280
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
91

5 7 9 11 13 15 17 19 
8
5 7 9 11 13 15 17 19 
93
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
5 7 9 11 13 15 17 19 
96
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."]	
24
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."]	
25

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."]	
291____2292

5 7 9 11 13 15 17 19 
9
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."]	
294
5 7 9 11 13 15 17 19 
8
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."]	
296
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."]	
298
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."]	
299
5 7 9 11 13 15 17 19 
0
5 7 9 11 13 15 17 19 
83
5 7 9 11 13 15 17 19 
2
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
4
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
6
5 7 9 11 13 15 17 19 
07

Đầ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 19 
83______93____94
5 7 9 11 13 15 17 19 
86
5 7 9 11 13 15 17 19 
3
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."]	
246

5 7 9 11 13 15 17 19 
8
5 7 9 11 13 15 17 19 
93
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
5 7 9 11 13 15 17 19 
96
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."]	
24
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."]	
25

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."]	
291
5 7 9 11 13 15 17 19 
0
5 7 9 11 13 15 17 19 
93
5 7 9 11 13 15 17 19 
2
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
6
5 7 9 11 13 15 17 19 
7

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."]	
26
5 7 9 11 13 15 17 19 
32

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."]	
291
5 7 9 11 13 15 17 19 
19
5 7 9 11 13 15 17 19 
7

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."]	
26
5 7 9 11 13 15 17 19 
9
5 7 9 11 13 15 17 19 
38
5 7 9 11 13 15 17 19 
3
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
2
Enter the start of range: 3
Enter the end of range: 11
3 5 7 9 11 
3

Đầ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 11 
4

5 7 9 11 13 15 17 19 
43

[5, 7, 9, 11, 13, 15]
7

5 7 9 11 13 15 17 19 
83____93____94
5 7 9 11 13 15 17 19 
48

5 7 9 11 13 15 17 19 
49
5 7 9 11 13 15 17 19 
3____2246
5 7 9 11 13 15 17 19 
48

 

5 7 9 11 13 15 17 19 
58

5 7 9 11 13 15 17 19 
54
5 7 9 11 13 15 17 19 
3
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_______88
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."]	
209
5 7 9 11 13 15 17 19 
59
5 7 9 11 13 15 17 19 
2
5 7 9 11 13 15 17 19 
3
5 7 9 11 13 15 17 19 
5
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
5 7 9 11 13 15 17 19 
64
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."]	
24
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

Làm cách nào tôi có thể in các số lẻ từ 1 đến 100 để sử dụng trong vòng lặp Python?

Python3 .
start = int [ input [ "Nhập phần đầu của dãy. " ]] end = int [ input [ "Nhập phần cuối của dãy. " ]]
# lặp lại từng số trong danh sách. cho num trong phạm vi [bắt đầu, kết thúc + 1]
# kiểm tra điều kiện. nếu số % 2. = 0. in [số]

Có bao nhiêu số chẵn từ 1 đến 100 trong Python?

50 các số chẵn từ 1 đến 100.

Làm cách nào để in số chẵn và số lẻ trong Python bằng vòng lặp for?

Thuật toán in các số chẵn và số lẻ từ 1 đến N .
Sử dụng hàm input[] của python cho phép người dùng nhập giá trị giới hạn tối đa
Tiếp theo, Chạy một vòng lặp và Thêm giá trị hiện tại của n vào biến num
Tiếp theo, Python sẽ in các số chẵn và lẻ từ 1 đến khi người dùng nhập giá trị giới hạn tối đa

Chủ Đề