Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用+;=在while循环中,但需要排除最大值_Python_Python 3.x - Fatal编程技术网

Python 使用+;=在while循环中,但需要排除最大值

Python 使用+;=在while循环中,但需要排除最大值,python,python-3.x,Python,Python 3.x,我的任务是创建一个程序,将所有输入的数字相加,除了列表中最高的整数。我假设使用while和if-then逻辑,但我不知道如何排除最大数。当字符串“end”被放入控制台时,我还必须使程序中断。到目前为止, total = 0 while 1 >= 1 : value = input("Enter the next number: ") if value != "end": num = float(value) total += num

我的任务是创建一个程序,将所有输入的数字相加,除了列表中最高的整数。我假设使用while和if-then逻辑,但我不知道如何排除最大数。当字符串“end”被放入控制台时,我还必须使程序中断。到目前为止,

total = 0
while 1 >= 1 :
    value = input("Enter the next number: ")
    if value != "end":
        num = float(value)
        total += num
    if value == 'end':
        print("The sum of all values except for the maximum value is: ",total)
        return total
        break

我只是不知道如何让它忽略输入的最高数字。提前谢谢!我正在使用Python3供参考。

这就是您想要做的吗

total = 0
maxValue = None
while True:
    value = input("Enter the next number: ")
    if value != "end":
        num = float(value)
        maxValue = num if maxValue and num > maxValue else num
        total += num
    else:
        print("The sum of all values except for the maximum value is: ",total-maxValue )
        # return outside a function is SyntaxError
        break

在这里,你可以保持它接近你原来的。在python中,使用列表对于这类事情非常有用

list = []

while True:
    num = input("Please enter value")
    if num == "end":
        list.remove(max(list))
        return sum(list)
    else:
        list.append(int(num))
如果您输入1、2和3,这将输出3-它将1和2相加,并丢弃原始3

你说过这是一项任务,所以如果不允许列出清单,那么你可以使用

max = 0
total = 0
while True:
    num = input("Please enter value")
    if str(num) == "end":
        return total - max
    if max < int(num):
        max = int(num)
    total += int(num)
max=0
总数=0
尽管如此:
num=输入(“请输入值”)
如果str(num)=“end”:
返回总数-最大值
如果最大值
实现所需结果的最简单方法是使用python的内置max函数(也就是说,如果您不关心性能,因为这种方法实际上是在列表上迭代两次而不是一次)


这与您希望它做的并不完全相同,但结果将是相同的(因为您不需要添加最大的项目,而可以在最后减去它)。

这应该适合您

total = 0
highest = None
while True:
    value = input("Enter the next number: ")
    if value != 'end':
        num = float(value)
        if highest is None or num > highest:
            highest = num
        total += num
    else:
        break
print("The sum of all values except for the maximum value is: ",total-highest )
print(total-highest)

你为什么不把它们加起来,记下最大值,最后从总净值中减去最大值呢?我在你的问题中遗漏了什么吗?我无法手动将它们全部相加,因为我必须将其保持在无限循环中,直到用户键入“end”。编辑:只是想了想你的答案。那会有用的,但我不知道如何告诉程序跟踪最高的数字。有办法做到这一点吗?没有,我的意思是只需添加一行来跟踪最大值,因为您需要输入,并且在最后返回时,在答案中也添加了一个检查。对于max=False-y值,它可能不起作用,但您可以正确地计算出来吗?:)Python中的无限循环是
,而True:
。另外,如果使用
else
,两个if子句会更清晰。也就是说,你的家庭作业描述中没有一项建议你必须对循环中的数字进行汇总,你也可以将它们存储在一个列表中,去掉最大值,然后进行汇总。顺便说一句:Python中甚至有一个
sum()
函数来实现这一点!您还应该打印正确的值:
print(“除了最大值之外的所有值的总和是:”,total-max)
我没有看到其他地方可以这样做。更正。谢谢你!我被困在逻辑上,并不真正了解max函数。我对python真的很陌生,所以我需要一段时间才能习惯这种逻辑。@George这里没有使用
max
函数。
return
函数的外部是
SyntaxError
Hmm。对我来说,累加一个总数和一个最大值,然后减去最大值似乎要干净得多。另外,我们可以教运算吗(他显然是个初学者)python中的无限循环拼写为
,而True:
请。同意这一说法,但我始终发现易于操作列表是python最好的部分之一,因此OP可以看到这一点。但我将更新为非列表版本,这可能会违反OP的指定。
total = 0
highest = None
while True:
    value = input("Enter the next number: ")
    if value != 'end':
        num = float(value)
        if highest is None or num > highest:
            highest = num
        total += num
    else:
        break
print("The sum of all values except for the maximum value is: ",total-highest )
print(total-highest)