Hướng dẫn python while max - python trong khi tối đa

Dưới hai trong khi các mã vòng để tìm các giá trị tối đa và tối thiểu từ một số nhất định. Mã đầu tiên không hoạt động, đầu ra là sai, mà tôi không hiểu tại sao?

Show

Nội dung chính ShowShow

  • 1. Trong khi vòng lặp
  • 2. Dòng điều khiển vòng lặp: Khác
  • 3. Lưu lượng điều khiển vòng lặp: Tiếp tục
  • 4. Nhiều bài tập
  • Max () và Min () là gì?
  • Làm thế nào để bạn tìm thấy Min và Max trong Python?
  • Có phải trong vòng lặp Pythonic?
  • Độ phức tạp thời gian của phương thức Min () và Max () trong Python là gì?

cái thứ hai đang hoạt động, sau khi tôi giải mã đầu vào là một số nguyên; Nếu ai đó có thể giải thích cho tôi, làm thế nào điều này tạo ra sự khác biệt. Cảm ơn

Mã1:

 largest = None
 smallest = None

 while True:

     num = input("Enter a number: ")

     if num == "done":
         break

     if largest is None or num> largest:
         largest = num
     elif smallest is None or num< smallest:
         smallest = num

 print ("Maximum is", largest)
 print ("Minimum is", smallest)

Code2

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)

Đã hỏi ngày 31 tháng 10 năm 2019 lúc 18:01Oct 31, 2019 at 18:01Oct 31, 2019 at 18:01

Hướng dẫn python while max - python trong khi tối đa

3

Đầu ra cho "Mã 1" khác với những gì bạn mong đợi bởi vì các toán hạng trong so sánh là

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
6, theo đó thứ tự được xác định khác với thứ tự của
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
7 tương ứng.

Nói cách khác, nếu

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
8 và
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
9 là hai
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
7 sao cho
while some condition:
    a block of statements
1, thì chúng ta có thể chắc chắn rằng
while some condition:
    a block of statements
2 chỉ khi độ dài của hai chuỗi là giống nhau, nếu không nó có thể xảy ra
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
9.

Thứ tự của chuỗi bao gồm các số là "bảng chữ cái" trên các ký tự xuất hiện đầu tiên, do đó, trong khi

while some condition:
    a block of statements
7, v.v., khi hai chuỗi có nhiều ký tự, một trong hai tùy chọn được mở, ví dụ:
  • cho
    while some condition:
        a block of statements
    
    8 và
    while some condition:
        a block of statements
    
    9,
     largest = None
     smallest = None
     while True:
         try:
             num = input("Enter the number: ")
             if num == "done":
                 break
             num = int(num)
             if largest is None or largest < num:
                 largest = num
             elif smallest is None or smallest > num:
                 smallest = num
         except:
             print("Invalid input")
    
     print ("Maximum is", largest)
     print ("Minimum is", smallest)
    
    30 (đối diện
     largest = None
     smallest = None
     while True:
         try:
             num = input("Enter the number: ")
             if num == "done":
                 break
             num = int(num)
             if largest is None or largest < num:
                 largest = num
             elif smallest is None or smallest > num:
                 smallest = num
         except:
             print("Invalid input")
    
     print ("Maximum is", largest)
     print ("Minimum is", smallest)
    
    31) vì
     largest = None
     smallest = None
     while True:
         try:
             num = input("Enter the number: ")
             if num == "done":
                 break
             num = int(num)
             if largest is None or largest < num:
                 largest = num
             elif smallest is None or smallest > num:
                 smallest = num
         except:
             print("Invalid input")
    
     print ("Maximum is", largest)
     print ("Minimum is", smallest)
    
    32 và sự hiện diện của
     largest = None
     smallest = None
     while True:
         try:
             num = input("Enter the number: ")
             if num == "done":
                 break
             num = int(num)
             if largest is None or largest < num:
                 largest = num
             elif smallest is None or smallest > num:
                 smallest = num
         except:
             print("Invalid input")
    
     print ("Maximum is", largest)
     print ("Minimum is", smallest)
    
    33 sau khi
     largest = None
     smallest = None
     while True:
         try:
             num = input("Enter the number: ")
             if num == "done":
                 break
             num = int(num)
             if largest is None or largest < num:
                 largest = num
             elif smallest is None or smallest > num:
                 smallest = num
         except:
             print("Invalid input")
    
     print ("Maximum is", largest)
     print ("Minimum is", smallest)
    
    34 không liên quan
  • Đối với
    while some condition:
        a block of statements
    
    8 và
     largest = None
     smallest = None
     while True:
         try:
             num = input("Enter the number: ")
             if num == "done":
                 break
             num = int(num)
             if largest is None or largest < num:
                 largest = num
             elif smallest is None or smallest > num:
                 smallest = num
         except:
             print("Invalid input")
    
     print ("Maximum is", largest)
     print ("Minimum is", smallest)
    
    36,
     largest = None
     smallest = None
     while True:
         try:
             num = input("Enter the number: ")
             if num == "done":
                 break
             num = int(num)
             if largest is None or largest < num:
                 largest = num
             elif smallest is None or smallest > num:
                 smallest = num
         except:
             print("Invalid input")
    
     print ("Maximum is", largest)
     print ("Minimum is", smallest)
    
    37 (giống như
     largest = None
     smallest = None
     while True:
         try:
             num = input("Enter the number: ")
             if num == "done":
                 break
             num = int(num)
             if largest is None or largest < num:
                 largest = num
             elif smallest is None or smallest > num:
                 smallest = num
         except:
             print("Invalid input")
    
     print ("Maximum is", largest)
     print ("Minimum is", smallest)
    
    38) vì
     largest = None
     smallest = None
     while True:
         try:
             num = input("Enter the number: ")
             if num == "done":
                 break
             num = int(num)
             if largest is None or largest < num:
                 largest = num
             elif smallest is None or smallest > num:
                 smallest = num
         except:
             print("Invalid input")
    
     print ("Maximum is", largest)
     print ("Minimum is", smallest)
    
    39 và sự hiện diện của
     largest = None
     smallest = None
     while True:
         try:
             num = input("Enter the number: ")
             if num == "done":
                 break
             num = int(num)
             if largest is None or largest < num:
                 largest = num
             elif smallest is None or smallest > num:
                 smallest = num
         except:
             print("Invalid input")
    
     print ("Maximum is", largest)
     print ("Minimum is", smallest)
    
    33 sau
    while some condition:
        a block of statements
    
    8 trong
     largest = None
     smallest = None
     while True:
         try:
             num = input("Enter the number: ")
             if num == "done":
                 break
             num = int(num)
             if largest is None or largest < num:
                 largest = num
             elif smallest is None or smallest > num:
                 smallest = num
         except:
             print("Invalid input")
    
     print ("Maximum is", largest)
     print ("Minimum is", smallest)
    
    36 làm cho
     largest = None
     smallest = None
     while True:
         try:
             num = input("Enter the number: ")
             if num == "done":
                 break
             num = int(num)
             if largest is None or largest < num:
                 largest = num
             elif smallest is None or smallest > num:
                 smallest = num
         except:
             print("Invalid input")
    
     print ("Maximum is", largest)
     print ("Minimum is", smallest)
    
    36 "lớn hơn".

Lưu ý rằng thứ tự/so sánh chuỗi thực sự hoạt động cho bất kỳ chuỗi nào, ngay cả những người không chứa số.

Đã trả lời ngày 31 tháng 10 năm 2019 lúc 20:19Oct 31, 2019 at 20:19Oct 31, 2019 at 20:19

norok2norok2norok2norok2

24.2k4 Huy hiệu vàng64 Huy hiệu bạc93 Huy hiệu Đồng4 gold badges64 silver badges93 bronze badges4 gold badges64 silver badges93 bronze badges

0

  • Học thuyết
  • Các bước
  • Các vấn đề

1. Trong khi vòng lặp

Vòng lặp

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
74 lặp lại chuỗi hành động nhiều lần cho đến khi một số điều kiện đánh giá là
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
75. Điều kiện được đưa ra trước thân vòng và được kiểm tra trước mỗi lần thực hiện thân vòng. Thông thường, vòng lặp
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
74 được sử dụng khi không thể xác định số lần lặp vòng chính xác.

Cú pháp của vòng lặp

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
74 trong trường hợp đơn giản nhất trông như thế này:
while some condition:
    a block of statements

Python trước tiên kiểm tra tình trạng. Nếu là sai, thì vòng lặp bị chấm dứt và điều khiển được chuyển sang câu lệnh tiếp theo sau thân vòng

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
74. Nếu điều kiện là đúng, thì phần thân vòng được thực thi và sau đó điều kiện được kiểm tra lại. Điều này tiếp tục trong khi điều kiện là đúng. Khi điều kiện trở thành sai, vòng lặp chấm dứt và điều khiển được chuyển sang câu lệnh tiếp theo sau vòng lặp.

Ví dụ: đoạn chương trình sau đây in các hình vuông của tất cả các số nguyên từ 1 đến 10. Ở đây người ta có thể thay thế vòng lặp "trong khi" bằng vòng lặp

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
79:
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
3

Trong ví dụ này, biến

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
50 bên trong vòng lặp lặp lại từ 1 đến 10. Một biến như vậy có giá trị thay đổi với mỗi lần lặp vòng lặp mới được gọi là bộ đếm. Lưu ý rằng sau khi thực hiện đoạn này, giá trị của biến
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
50 được xác định và bằng
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
52, bởi vì khi
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
53, điều kiện
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
54 là sai lần đầu tiên.

Dưới đây là một ví dụ khác sử dụng vòng lặp

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
74 để xác định số chữ số của số nguyên
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
56:
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
7

Trên mỗi lần lặp, chúng tôi cắt chữ số cuối cùng của số bằng cách sử dụng phân chia số nguyên xuống 10 (

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
57). Trong biến
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
58, chúng tôi đếm được bao nhiêu lần chúng tôi đã làm điều đó.

Trong Python, có một cách khác, dễ dàng hơn để giải quyết vấn đề này:

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
59.

Quảng cáo của Google, có thể dựa trên sở thích của bạn

2. Dòng điều khiển vòng lặp: Khác

Người ta có thể viết một câu lệnh

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
60 sau một thân vòng được thực hiện một lần sau khi kết thúc vòng lặp:
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
5

Ngay từ cái nhìn đầu tiên, tuyên bố này dường như không có ý nghĩa, bởi vì cơ thể tuyên bố

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
60 chỉ có thể được đặt sau khi kết thúc vòng lặp. Tuyên bố "Khác" sau một vòng lặp chỉ có ý nghĩa khi được sử dụng kết hợp với lệnh
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
62. Nếu trong quá trình thực hiện trình thông dịch Python vòng lặp
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
62, nó sẽ ngay lập tức dừng thực thi vòng lặp và thoát ra khỏi nó. Trong trường hợp này, nhánh
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
60 không được thực thi. Vì vậy,
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
62 được sử dụng để hủy bỏ việc thực hiện vòng lặp trong giữa bất kỳ lần lặp.

Dưới đây là một ví dụ giống như jack đen: một chương trình đọc các số và tổng nó cho đến khi tổng số lớn hơn hoặc bằng 21. Trình tự đầu vào kết thúc bằng 0 để chương trình có thể dừng ngay cả khi tổng số của tất cả các số là Ít hơn 21.

Hãy xem cách nó hoạt động trên các đầu vào khác nhau.

Phiên bản 1. Vòng lặp được thoát bình thường sau khi kiểm tra điều kiện, do đó, nhánh "khác" được thực thi. The loop is exited normally after checking the condition, so the "else" branch is executed. The loop is exited normally after checking the condition, so the "else" branch is executed.

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
6

Phiên bản 2. Vòng lặp bị hủy bỏ bởi

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
42, do đó, nhánh "khác" bị bỏ qua. The loop is aborted by 2, do đó, nhánh "khác" bị bỏ qua. The loop is aborted by

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
62, so the "else" branch is skipped.
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
6

Nhánh "khác" cũng có thể được sử dụng với vòng "cho". Chúng ta hãy xem ví dụ khi một chương trình đọc 5 số nguyên nhưng dừng lại ngay khi số nguyên âm đầu tiên được đáp ứng.

Phiên bản 1. Vòng lặp được thoát bình thường, vì vậy nhánh "khác" được thực thi. The loop is exited normally, so the "else" branch is executed. The loop is exited normally, so the "else" branch is executed.

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
7

Phiên bản 2. Vòng lặp bị hủy bỏ, vì vậy nhánh "khác" không được thực thi. The loop is aborted, so the "else" branch isn't executed. The loop is aborted, so the "else" branch isn't executed.

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
7

Quảng cáo của Google, có thể dựa trên sở thích của bạn

3. Lưu lượng điều khiển vòng lặp: Tiếp tục

Một hướng dẫn khác được sử dụng để kiểm soát thực thi vòng lặp là

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
67. Nếu thông dịch viên Python gặp
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
67 ở đâu đó ở giữa vòng lặp vòng, nó sẽ bỏ qua tất cả các hướng dẫn còn lại và tiến hành lần lặp tiếp theo.
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
0

Nếu

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
62 và
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
67 được đặt bên trong một số vòng lặp lồng nhau, chúng chỉ ảnh hưởng đến việc thực hiện cái trong cùng. Chúng ta hãy nhìn vào ví dụ khá ngớ ngẩn để chứng minh điều đó:
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
1

Các hướng dẫn

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
62 và
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
67 không được khuyến khích, nếu bạn có thể thực hiện ý tưởng của mình mà không cần sử dụng chúng. Dưới đây là một ví dụ điển hình về việc sử dụng xấu của
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
62: Mã này đếm số chữ số trong một số nguyên.
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
2

Nó sạch hơn và dễ đọc hơn để viết lại vòng lặp này với một điều kiện vòng lặp có ý nghĩa:

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
3

Quảng cáo của Google, có thể dựa trên sở thích của bạn

3. Lưu lượng điều khiển vòng lặp: Tiếp tục

Một hướng dẫn khác được sử dụng để kiểm soát thực thi vòng lặp là

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
67. Nếu thông dịch viên Python gặp
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
67 ở đâu đó ở giữa vòng lặp vòng, nó sẽ bỏ qua tất cả các hướng dẫn còn lại và tiến hành lần lặp tiếp theo.
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
0

Nếu

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
62 và
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
67 được đặt bên trong một số vòng lặp lồng nhau, chúng chỉ ảnh hưởng đến việc thực hiện cái trong cùng. Chúng ta hãy nhìn vào ví dụ khá ngớ ngẩn để chứng minh điều đó:
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
1

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
4

Các hướng dẫn

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
5

 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
62 và
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
67 không được khuyến khích, nếu bạn có thể thực hiện ý tưởng của mình mà không cần sử dụng chúng. Dưới đây là một ví dụ điển hình về việc sử dụng xấu của
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
62: Mã này đếm số chữ số trong một số nguyên.
 largest = None
 smallest = None
 while True:
     try:
         num = input("Enter the number: ")
         if num == "done":
             break
         num = int(num)
         if largest is None or largest < num:
             largest = num
         elif smallest is None or smallest > num:
             smallest = num
     except:
         print("Invalid input")

 print ("Maximum is", largest)
 print ("Minimum is", smallest)
2

Quảng cáo của Google, có thể dựa trên sở thích của bạn

3. Lưu lượng điều khiển vòng lặp: Tiếp tục

Một hướng dẫn khác được sử dụng để kiểm soát thực thi vòng lặp là . Obviously, it is easiest to determine the min and max if the data are ordered from lowest to highest. So for our data, the min is 13 and the max is 110.

largest = None smallest = None while True: try: num = input("Enter the number: ") if num == "done": break num = int(num) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except: print("Invalid input") print ("Maximum is", largest) print ("Minimum is", smallest) 67. Nếu thông dịch viên Python gặp largest = None smallest = None while True: try: num = input("Enter the number: ") if num == "done": break num = int(num) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except: print("Invalid input") print ("Maximum is", largest) print ("Minimum is", smallest) 67 ở đâu đó ở giữa vòng lặp vòng, nó sẽ bỏ qua tất cả các hướng dẫn còn lại và tiến hành lần lặp tiếp theo. largest = None smallest = None while True: try: num = input("Enter the number: ") if num == "done": break num = int(num) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except: print("Invalid input") print ("Maximum is", largest) print ("Minimum is", smallest) 0

Nếu . Call min() and max() with a single iterable or with any number of regular arguments. Use min() and max() with strings and dictionaries.

largest = None smallest = None while True: try: num = input("Enter the number: ") if num == "done": break num = int(num) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except: print("Invalid input") print ("Maximum is", largest) print ("Minimum is", smallest) 62 và largest = None smallest = None while True: try: num = input("Enter the number: ") if num == "done": break num = int(num) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except: print("Invalid input") print ("Maximum is", largest) print ("Minimum is", smallest) 67 được đặt bên trong một số vòng lặp lồng nhau, chúng chỉ ảnh hưởng đến việc thực hiện cái trong cùng. Chúng ta hãy nhìn vào ví dụ khá ngớ ngẩn để chứng minh điều đó: largest = None smallest = None while True: try: num = input("Enter the number: ") if num == "done": break num = int(num) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except: print("Invalid input") print ("Maximum is", largest) print ("Minimum is", smallest) 1

Các hướng dẫn used to iterate over a block of code as long as the test expression (condition) is true. We generally use this loop when we don't know the number of times to iterate beforehand.

largest = None smallest = None while True: try: num = input("Enter the number: ") if num == "done": break num = int(num) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except: print("Invalid input") print ("Maximum is", largest) print ("Minimum is", smallest) 62 và largest = None smallest = None while True: try: num = input("Enter the number: ") if num == "done": break num = int(num) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except: print("Invalid input") print ("Maximum is", largest) print ("Minimum is", smallest) 67 không được khuyến khích, nếu bạn có thể thực hiện ý tưởng của mình mà không cần sử dụng chúng. Dưới đây là một ví dụ điển hình về việc sử dụng xấu của largest = None smallest = None while True: try: num = input("Enter the number: ") if num == "done": break num = int(num) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except: print("Invalid input") print ("Maximum is", largest) print ("Minimum is", smallest) 62: Mã này đếm số chữ số trong một số nguyên. largest = None smallest = None while True: try: num = input("Enter the number: ") if num == "done": break num = int(num) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except: print("Invalid input") print ("Maximum is", largest) print ("Minimum is", smallest) 2

Nó sạch hơn và dễ đọc hơn để viết lại vòng lặp này với một điều kiện vòng lặp có ý nghĩa:O(m), so the complexity of min() and max() is O(m).