简体   繁体   English

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

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

Design a program with a loop that lets the user enter a series of numbers.设计一个带有循环的程序,让用户输入一系列数字。 The user should enter - 99 to signal the end of the series.用户应输入 - 99 以表示系列结束。 After all the numbers have been enter been entered, the program should display the largest and smallest numbers entered.输入所有数字后,程序应显示输入的最大和最小数字。

when I go to run my program and enter a number like "4" it returns saying the smallest and largest number is 4. But when I want to put a series of numbers like "3,4,5" it gives an error.当我 go 运行我的程序并输入一个像“4”这样的数字时,它返回说最小和最大的数字是 4。但是当我想输入一系列像“3、4、5”这样的数字时,它会出错。 I don't know if I am typing it wrong or if there is an issue with my program.不知道是我打错了还是我的程序有问题。 Let me know what you guys think让我知道你们的想法

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(largest, smallest): def displayLargestSmallest(最大,最小):

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

You are complicating things, now you will receive a set of numbers in an array, and you will extract the largest number, its number, and the smallest number and print it as follows:你把事情复杂化了,现在你将收到数组中的一组数字,你将提取最大的数字、它的数字和最小的数字并按如下方式打印: 在此处输入图像描述

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

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