简体   繁体   中英

Write a program to find the greatest number in a list using python?

My solution is:

numbers = [1, 2, 3, 5, 9, 6, 101, 55, 7, 1, 3, 88, 99, 101, 6, 88, 66, 101, 6, 101, 55, 1001]
n = len(numbers)
for x in range(n):
    y = 0
    while y < n:
        if numbers[x] >= numbers[y]:
            y += 1
        else:
            break
    else:
       z = x
print(f'Greatest number = {numbers[z]}')

I know it's complicated but is it right? And one more thing --- Even though I am getting my answers correct for every list of numbers i can make but pycharm is showing a warning that -- Name 'z' can be undefined. why is that and how can I remove it//

yes, it's complicated and not effective to use two loops just to find the max. Even max function can be used

numbers = [1, 2, 3, 5, 9, 6, 101, 55, 7, 1, 3, 88, 99, 101, 6, 88, 66, 6, 101, 55, 1001]
print(max(numbers))

For writing your own logic, this is in fact simpler.

numbers = [1, 2, 3, 5, 9, 6, 101, 55, 7, 1, 3, 88, 99, 101, 6, 88, 66, 6, 101, 55, 1001]
maxi = numbers[0]
for i in numbers:
    if i > maxi:
        maxi = i
print("Greatest number: ", maxi)

The else: block is only executed if the while loop ends normally, rather than by executing a break statement.

Since you only set z in the else: block, if the loop ends due to the break statement, z won't be set.

It may be that the mathematical logic of the two loops ensures that at least one of the inner loops will complete without executing break . But PyCharm can't tell that this is always true, so it warns about it.

If you're certain that there's no possibility that the loop will end without setting z , you can suppress the warning in PyCharm.

max_number = max(numbers)
print('Greatest number = {}'.format(max_number))

For pycharm warning, it is because the variable z defined and assigned in else block, unlike like java python is dynamically typed and doesn't require variable declaration

There is no point in doing z = x. Just do:

numbers = [1, 2, 3, 5, 9, 6, 101, 55, 7, 1, 3, 88, 99, 101, 6, 88, 66, 101, 6, 101, 55, 1001]
n = len(numbers)
for x in range(n):
    y = 0
    while y < n:
        if numbers[x] >= numbers[y]:
            y += 1
        else:
            break

print(f'Greatest number = {numbers[z]}')

Looks like you are using C/C++ approach to solve it by python has a lot of built-in functions that can make your life much easier. In this case, you can use max() function.

The shortest way

numbers = [1, 2, 3, 5, 9, 6, 101, 55, 7, 1, 3, 88, 99, 101, 6, 88, 66, 101, 6, 101, 55, 1001]

print(f'Greatest number = {max(numbers)}')
Greatest number = 1001

You recieve this notification because z variable is declared only in else block. And if your code will exit from while block (not in this case, but anyway) and never goes to else , you will recieve an error:

UnboundLocalError: local variable 'z' referenced before assignment

Simple example:

def test(x):
   ...:     if x == 1:
   ...:         z = x
   ...:     else:
   ...:         pass
   ...:     print(z)
   ...:     
test(1)
1
test(2)
Traceback (most recent call last):
  File "/home/terin/regru/venvs/.dl/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-13-14ddcd275974>", line 1, in <module>
    test(2)
  File "<ipython-input-11-43eaa101b10d>", line 6, in test
    print(z)
UnboundLocalError: local variable 'z' referenced before assignment

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