简体   繁体   English

如何找到列表中没有的最大数字?

[英]How do i find the biggest number that are not in a list?

I am trying to make a program that gives you the average, total, and biggest number entered. 我正在尝试制作一个程序,为您提供输入的平均数,总数和最大数。 I am stuck at the biggest number part. 我被困在最大的部分。 My numbers are not in a list, so I don't know how to find the biggest one. 我的电话号码不在列表中,所以我不知道如何找到最大的电话号码。

num=0 
total=0 
average=0 
count=0 

while True: 
  num=input("enter a number:")
  num=int(num) 
  if num==-999: 
    break 
  total=total + num
  count=count+1 
  biggest = max(total)

average=total/count 

#print the results
print("the total is:", total)
print("the biggest number is:", biggest)
print("the average is:", average)

I would like it to print the biggest number at the end. 我希望它最后打印最大的数字。

Thanks 谢谢

num=0 
total=0 
average=0 
count=0
biggest=0

while True: 
  num=input("enter a number:")
  num=int(num) 
  if num==-999: 
    break 
  total=total + num
  count=count+1 
  if num > biggest:
      biggest = num

average=total/count 

#print the results
print("the total is:", total)
print("the biggest number is:", biggest)
print("the average is:", average)

you can just: 您可以:

if num > biggest:
   biggest = num

initialize biggest with a value which can not be bigger than any input: 使用不能大于任何输入的值来初始化maximum:

import numpy
biggest = -np.inf

replace 更换

biggest = max(total)

by 通过

biggest = max(biggest, num)

Initialize a variable biggest before the loop: 初始化循环前biggest的变量:

biggest = -9999

This should be initialized to something smaller than any of the numbers that the program is going to encounter. 应该将其初始化为小于程序将要遇到的任何数字的东西。 The above assumes that -9999 is such a number which is a bad assumption in general; 以上假设-9999是一个数字,这通常是一个错误的假设; there are better ways to accomplish that, eg one of the comments suggests 有更好的方法可以做到这一点,例如其中一项评论表明

import sys
biggest = -sys.maxint

That's the negative of the maximum integer value that this implementation can represent. 这是此实现可以表示的最大整数值的负数。

Then in the body of the loop, update the variable biggest : 然后在循环的主体中,更新变量biggest

biggest = max(biggest, num)

If num is bigger than biggest then max(biggest, num) will return the value of num so that biggest will get the value of num . 如果num是大于biggest然后max(biggest, num)将返回的值num ,使biggest将获得的价值num If it is smaller, biggest will stay unchanged. 如果较小,则biggest将保持不变。 In other words, biggest remembers the largest value seen so far. 换句话说, biggest记住迄今为止看到的最大价值。 At the end of the loop, it will then hold the largest value seen, period. 在循环结束时,它将保持看到的最大值,即周期。

Here idited your code to be right 在这里将您的代码设置为正确

num=0 
total=0 
average=0 
count=0 
biggest = None  ### added

while True: 
  num=input("enter a number:")
  num=int(num)
  if num==-999: 
    break 
  total=total + num
  count=count+1 

  #biggest = max(total)   Here where was you wrong
  if num > biggest :   #### added
     biggest = num     #### added

average=total/count 

#print the results
print("the total is:", total)
print("the biggest number is:", biggest)
print("the average is:", average)

Let me share you a solution for your problem. 让我为您分享解决问题的方法。 (Take a look at the if clause after the check of the number being "-999"). (检查数字为“ -999”后,看看if子句)。 Feel free to ask if you have any questions! 随时问您有什么问题! Hope it helps you 希望对您有帮助

num=0 
total=0 
average=0 
count=0
biggest=0 

while True: 
  num=input("enter a number:")
  num=int(num) 
  if num==-999: 
    break
  if num > biggest:
    biggest = num
  total=total + num
  count=count+1 

average=total/count 

#print the results
print("the total is:", total)
print("the biggest number is:", biggest)
print("the average is:", average)

If you look into some other methods you can get a lot more functionality and do even more with your program, take a look at this code and try to take some ideas it could make your task and future tasks easier for you 如果您研究其他方法,则可以获得更多功能,并且对程序进行更多操作,请看一下此代码,并尝试一些想法,以使您的任务和以后的任务更容易

numbers = []

print("Enter any non-number to perform calculations.\n")

while True:
    try:
        num = int(input("Enter a number: "))
        numbers.append(num)

    except ValueError:
        break

total = sum(numbers)
average = total/len(numbers)
biggest = max(numbers)

print(f"\nThe total is: {total}")
print(f"The biggest number is: {biggest}")
print(f"The average is: {average}")
 (xenial)vash@localhost:~/python$ python3.7 biggest.py Enter any non-number to perform calculations. Enter a number: 1 Enter a number: 2 Enter a number: 3 Enter a number: 4 Enter a number: -1 Enter a number: 2 Enter a number: 3 Enter a number: q The total is: 14 The biggest number is: 4 The average is: 2.0 

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

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