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

Python示例:打印范围内负数

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

Python 示例

写一个 Python示例打印一个范围内的负数。这个 Python 示例允许开始和结束数字,并打印该范围内的负数。

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

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

Python示例,使用 while 循环打印范围为 1 到 n 的负数。

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

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

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

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

def negativeNumbers(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 Negative Numbers from {0} and {1}".format(minimum, maximum)) 
negativeNumbers(minimum, maximum)
Enter the Minimum Number = -25
Enter the Maximum Number = 120

All Negative Numbers from -25 and 120
-25   -24   -23   -22   -21   -20   -19   -18   -17   -16   -15   -14   -13   -12   -11   -10   -9   -8   -7   -6   -5   -4   -3   -2   -1 
本文固定链接:https://6yhj.com/leku-p-4270.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: 智能AI