Hướng dẫn dùng integers examples python
Ngày đăng:
04/09/2022
Trả lời:
0
Lượt xem:
181
Viết chương trình Python để đếm số dương và số âm trong Tuple bằng cách sử dụng phạm vi vòng lặp for. Điều kiện if (if (posngTuple[i] > = 0)) kiểm tra xem mục Tuple lớn hơn hoặc bằng 0. Nếu Đúng, chúng tôi thêm một vào số Tuple Tích cực; nếu không (tNegativeCount = tNegativeCount + 1), hãy thêm một vào giá trị số Tuple Phủ định. Trong ví dụ Tích cực và Phủ định Python này, chúng tôi đã sử dụng vòng lặp for (cho pntup trong posngTuple) để lặp lại các giá trị tuple thực tế và kiểm tra xem chúng lớn hơn hay bằng 0. Chương trình Python để đếm Tích cực và Phủ định trong Tuple bằng cách sử dụng Vòng lặp While. Trong ví dụ Python Tuple này, chúng tôi đã tạo một hàm trả về số lượng số Dương và Số âm. # Count of Tuple Positive and Negative Numbers def CountOfPositiveNegativeNumbers(evodTuple): tPositiveCount = tNegativeCount = 0 for pntup in evodTuple: if(pntup >= 0): tPositiveCount = tPositiveCount + 1 else: tNegativeCount = tNegativeCount + 1 return tPositiveCount, tNegativeCount evodTuple = (26, -77, -99, 75, 14, -56, 19, 81, -1, 33) print("Positive and Negative Tuple Items = ", evodTuple) PositiveCount, NegativeCount = CountOfPositiveNegativeNumbers(evodTuple) print("The Count of Positive Numbers in evodTuple = ", PositiveCount) print("The Count of Negative Numbers in evodTuple = ", NegativeCount)Positive and Negative Tuple Items = (26, -77, -99, 75, 14, -56, 19, 81, -1, 33) The Count of Positive Numbers in evodTuple = 6 The Count of Negative Numbers in evodTuple = 4 |