Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

โปรแกรม Python ตรวจสอบว่าค่าทั้งหมดในลิสต์มากกว่าค่าที่กำหนดหรือไม่


ตรวจสอบว่าทุกค่าในลิสต์มากกว่าค่าที่กำหนด (val) หรือไม่

ตัวอย่าง

A=[10,20,30,40,50], val=20 -> No (10 ไม่มากกว่า 20)
A=[10,20,30,40,50], val=5 -> Yes

อัลกอริทึม

  1. วนลูปทุกอิลิเมนต์ในลิสต์
  2. ถ้าพบค่า <= val ให้คืน False ทันที
  3. จบลูปคืน True

โค้ดตัวอย่าง

def checknumber(A, val):
    for i in A:
        if val >= i:
            return False
    return True

A = []
n = int(input("Enter the size of the List: "))
print("Enter the Elements:")
for _ in range(n):
    A.append(int(input()))
val = int(input("Enter the checking value: "))
if checknumber(A, val):
    print("All values are greater than", val)
else:
    print("Values are not greater than", val)

ผลลัพธ์

Enter the size of the List: 5
Enter the Elements: 10 20 30 40 50
Enter the checking value: 10
Values are not greater than 10