1

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)
5
  • do you want someone to do your homework? what have you done? Commented Oct 7, 2012 at 4:24
  • Please provide code samples of what you have tried. Commented Oct 7, 2012 at 4: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 Commented Oct 7, 2012 at 4: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. Commented Oct 7, 2012 at 7:18
  • Does this answer your question? Find the greatest number in a list of numbers Commented Mar 8, 2023 at 16:21

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

He said he was asked to write an algorithm so he could learn Programming.
"must be done using loop statements in python 3.2.3" :) also how do you close a question? lock it perhaps? just wondering
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.