简体   繁体   中英

What is the error in my code? I am trying to use the function to find the biggest number from the array given

This is my first time trying to pass values from an list into a function so still not 100% sure how it works, and I was wondering if anyone could tell me where I went wrong. This code is trying to find the biggest value out of the list. Would using the max() function make things any easier?

x = 1
numbers=[34,23,65,435,34,56,67,454,34,2]
length = int(len(numbers))
realLength = length -1
biggestNumber = numbers[0]
def displayBiggest(numbers):
  for x in range(realLength):
    if numbers[x] > biggestNumber:
      biggestNumber == numbers[x]
    x = x + 1
  

displayBiggest(numbers)
print(biggestNumber)

biggestNumber == numbers[x] - this is a comparison. You want biggestNumber = numbers[x] .

Also, you want to use global biggestNumber at the start of the function.


That would work, but really you would want to make everything local.

def displayBiggest(nums):
    biggest = nums[0]
    for i in nums[1:]:
        if i > biggest:
            biggest = i
    return biggest

biggestNumber = displayBiggest(numbers)

And, if you're able to, just use the built-in max function:

biggestNumber = max(numbers)

  1. You can directly use inbuilt max() function to get the largest number from an array like below:
numbers=[34,23,65,435,34,56,67,454,34,2]
biggestNumber = max(numbers)
print(biggestNumber)

  1. If you want to fix your code follow these steps
  • use assignment operator (=) instead of comparator (==) as biggestNumber = numbers[x]
  • After correcting the assignment operator you would get the error as local variable 'biggestNumber' referenced before assignment because you have declared variable biggestNumber outside the function so it's a global variable you can directly access global variable's value as in your code, whereas to change global variables value we need to explicitly mention that we are using global variable using global keyword inside the function, so now the code looks as below
x = 1
numbers=[34,23,65,435,34,56,67,454,34,2]
length = int(len(numbers))
realLength = length -1
biggestNumber = numbers[0]
def displayBiggest(numbers):
  global biggestNumber
  for x in range(realLength):
    if numbers[x] > biggestNumber:
      biggestNumber = numbers[x]
    x = x + 1

displayBiggest(numbers)
print(biggestNumber)

Well, you have 2 language errors, 1 logic error, and a few minor issues:D

The language errors are both in biggestNumber == numbers[x] :

  • first, the double equal sign, not an assignment; your code will check if biggestNumber is equal to numbers[x] but then the result is not assigned to anything so it is just thrown away
  • second, you didn't declare the variable as global . So once you have corrected == in = you will assign to a local variable which has nothing to do with the external one. Add global biggestNumber before you assign it

The logic error is in for x in range(realLength): :

  • range() will return numbers up to but not including the stop value, so length is the value we want. If the last number were the biggest you would not find it

Now the minor issues:

  • x = 1 and x = x + 1 are useless: the for loop takes care of its control variable (in this case they are just useless, but there are situations in which tampering with a control variable can cause a bug)
  • len() already returns an integer so there's no need to do `int(len(numbers))
  • although you can use a global , this is almost never a good idea: handle the variable in the function, and return it to the caller
  • instead of accessing the items in your list by index you could directly use them in your loop
  • you could start the loop from the second item (the one at index 1) since you already handled the first one

To sum up, I would change your code this way:

numbers=[34,23,65,435,34,56,67,454,34,2]
def displayBiggest(numbers):
  biggestNumber = numbers[0]
  for n in numbers[1:]:
    if n > biggestNumber:
      biggestNumber == n
  return biggestNumber

displayBiggest(numbers) #will print 454

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