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?
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:01
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 statements1, thì chúng ta có thể chắc chắn rằng
while some condition: a block of statements2 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 statements7, 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,i = 1 while i