简体   繁体   中英

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:

import numpy
biggest = -np.inf

replace

biggest = max(total)

by

biggest = max(biggest, num)

Initialize a variable biggest before the loop:

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; 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 = 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 . If it is smaller, biggest will stay unchanged. In other words, biggest remembers the largest value seen so far. 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"). 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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