2

Basically we are given a list of numbers and we are asked to write an algorithm to find the largest number in the list, note: the numbers are not in order and may contain decimals and negative numbers. this must be done using loop statements in python 3.2.3 Thanks.

alist=[3,10,90,5,-2,4,18,45,100,1,6]
largest=int()
for large in alist:
    if large >= large+1:
       largest=large
print(largest)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
3MIN3M
  • 69
  • 1
  • 1
  • 9
  • do you want someone to do your homework? what have you done? – user1406062 Oct 07 '12 at 04:24
  • Please provide code samples of what you have tried. – krishnang Oct 07 '12 at 04:26
  • not a homework question lol, im curious as to whats the simplest way to go about this. new to programming here...the teacher showed it in a complicated way in class and i didnt get it :S – 3MIN3M Oct 07 '12 at 04:26
  • You have several problems with your code. 1) int() returns 0, so if you have a list of negative numbers then your code would print 0 instead of the largest number. 2) large will never be > or = to large+1 so largest will never change and your code will print 0 every time. You should set largest equal to the first element in the list like I did in my answer. you should be comparing large to largest instead of large+1. Then your code will work. – ajon Oct 07 '12 at 07:18
  • Does this answer your question? [Find the greatest number in a list of numbers](https://stackoverflow.com/questions/3090175/find-the-greatest-number-in-a-list-of-numbers) – Karl Knechtel Mar 08 '23 at 16:21

2 Answers2

3

there's also a built in function called max... works like a charm

Sheena
  • 15,590
  • 14
  • 75
  • 113
  • He said he was asked to write an algorithm so he could learn Programming. – ajon Oct 09 '12 at 07:12
  • "must be done using loop statements in python 3.2.3" :) also how do you close a question? lock it perhaps? just wondering – 3MIN3M Oct 13 '12 at 19:14
0

This question was asked 9 years ago but I'm giving my answer because the question is still relevant today

we can do this for both numbers and strings

A) Finding the largest number in a given list:

your_list = [54, 26, 29, 48, 56, 32, 15, 17]
largest_num = -99999999 # Any value below zero is ok if you know there 
                        # are larger numbers in the list
    
for i in your_list:     # Checking every item in the list
    print("current number:", i) # Printing every item in the list 
                                # regardless of their value
    if i > largest_num: # Is it larger than -99999999?!
        largest_num = i # then value of largest number should be equal 
                        # to the value of i(as integer) 
print("largest number:",largest_num) # Let's print the result when 
                                     #  we're done

B) Finding the largest string in a given list:

my_list = ["a", "b", "c", "A", "B", "C", " "]
largest_str = my_list[0]
for i in my_list:
    print("current str:", i)
    if i > largest_str:
        largest_str = i

print("largest_str:", largest_str)