Python 程序:打印范围内负数

创新互联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

分享标题:Python 程序:打印范围内负数
文章链接:http://www.hantingmc.com/qtweb/news16/475666.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联