繁体   English   中英

查找最大和最小数字代码不适用于 python

[英]Find Largest and Smallest number code not working on python

设计一个带有循环的程序,让用户输入一系列数字。 用户应输入 - 99 以表示系列结束。 输入所有数字后,程序应显示输入的最大和最小数字。

当我 go 运行我的程序并输入一个像“4”这样的数字时,它返回说最小和最大的数字是 4。但是当我想输入一系列像“3、4、5”这样的数字时,它会出错。 不知道是我打错了还是我的程序有问题。 让我知道你们的想法

def main():
displayWelcome()
inputNum = int(); smallest = int(); largest = int()
smallest = None
largest = None
SENTINEL = -99
# program description

while True:
    inputNum=int(input('Enter a number (Zero indicates end of input): '))

    if inputNum == SENTINEL:
        print('End of program')
        break
    if smallest is None or inputNum < smallest:
        smallest = inputNum
    if largest is None or inputNum > largest:
        largest = inputNum
        
    displayLargestSmallest(largest, smallest)

def displayWelcome():
print ('This program displays the largest and smallest numbers \
entered by the user from a series of numbers')

def displayLargestSmallest(最大,最小):

    #module to display numbers input
    print('The largest number input is ', largest)
    print('The smallest number input is', smallest)

你把事情复杂化了,现在你将收到数组中的一组数字,你将提取最大的数字、它的数字和最小的数字并按如下方式打印: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM