当前分类:300例题>>正文

Python示例:打印范围内正数

来源:互联网   更新时间:2023年6月17日  

Python 示例

编写一个 Python示例来打印一个范围或从 1 到 n 的正数。这个 Python 示例允许最小和最大数字,并打印该范围内的正数。

minimum = int(input("Enter the Minimum Number = "))
maximum = int(input("Enter the Maximum Number = "))

print("\nAll Positive Numbers from {0} and {1}".format(minimum, maximum)) 
for num in range(minimum, maximum + 1):
    if num >= 0:
        print(num, end = '   ')

Python示例使用 while 循环打印一个范围内的正数。

minimum = int(input("Enter the Minimum Number = "))
maximum = int(input("Enter the Maximum Number = "))

print("\nAll Positive Numbers from {0} and {1}".format(minimum, maximum))
while minimum <= maximum:
    if minimum >= 0:
        print(minimum, end = '   ')
    minimum = minimum + 1
Enter the Minimum Number = -50
Enter the Maximum Number = 39

All Positive Numbers from -50 and 39
0   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39 

在这个 Python 示例中,positiveNumbers 函数接受两个整数并打印它们之间的正数。

def positiveNumbers(x, y):
    for num in range(x, y + 1):
        if num >= 0:
            print(num, end = '   ')

minimum = int(input("Enter the Minimum Number = "))
maximum = int(input("Enter the Maximum Number = "))

print("\nAll Positive Numbers from {0} and {1}".format(minimum, maximum)) 
positiveNumbers(minimum, maximum)
Enter the Minimum Number = -90
Enter the Maximum Number = 50

All Positive Numbers from -90 and 50
0   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50 
本文固定链接:https://6yhj.com/leku-p-4271.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: 推广