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)
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)
biggestNumber = numbers[x]
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 belowx = 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]
:
biggestNumber
is equal to numbers[x]
but then the result is not assigned to anything so it is just thrown awayglobal
. 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 itNow 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)) global
, this is almost never a good idea: handle the variable in the function, and return
it to the callerTo 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.