功能未输出正确的最大值

huangapple go评论60阅读模式
英文:

Function does not output correct maximum value

问题

I would like to find the maximum value z and find its i and j values at z maximum.

我想找到最大值 z 并找到 z 最大时的 i 和 j 值。

I tried the code using def function and did not get the correct maximum z value and its i and j values.

我尝试了使用 def 函数的代码,但没有得到正确的最大 z 值以及其对应的 i 和 j 值。

Below is my code:

以下是我的代码:

maxi = 0
def i_j():
global maxi
for i in range(1, 5):
for j in range(1, 5):
z = i + j
print(z)
if z > maxi:
maxi = z
return i, j
j += 1
i += 1
print("Maximum: {}".format(maxi))
x, y = i_j()
print("i at max:", x)
print("j at max:", y)

Python 代码的输出:

Python 代码的输出:

Maximum: 0
2
i at max: 1
j at max: 1

Kindly help me, many thanks.

请帮助我,非常感谢。

英文:

I would like to find the maximum value z and find its i and j values at z maximum.

I tried the code using def function and did not get the correct maximum z value and its i and j values.

Below is my code:

maxi = 0
def i_j ():
    global maxi
    for i in range (1, 5):
        for j in range (1, 5):
            z = i + j
            print(z)
            if z > maxi:
                maxi = z
                return i, j
            j += 1
        i += 1
print ("Maximum: {}".format(maxi))
x, y = i_j()
print ("i at max:", x)
print ("j at max:", y)

The output of the Python code:

Maximum: 0
2
i at max: 1
j at max: 1

Kindly help me, many thanks.

答案1

得分: 1

You are returning too early, this means you leave the function the first time that z > maxi is true, which means in the very first iteration.

Also, there are other mistakes that commenters pointed out:

  • maxi doesn't make much sense to print before calling the function.
  • The for loop automatically increases the loop variables (or rather, iterates through the list that is range(1,5,1)) and thus the incrementation doesn't actually do anything because it gets overwritten in the next step.

This is how it should look:

def i_j():
    maxi = 0
    maxi_i = 0
    maxi_j = 0
    for i in range(1, 5, 1):
        for j in range(1, 5, 1):
            z = i + j
            print("{}".format(z))
            if z > maxi:
                maxi = z
                maxi_i = i
                maxi_j = j
    return (maxi, maxi_i, maxi_j)

maxi, x, y = i_j()
print("Maximum: {}".format(maxi))
print("i at max: {}".format(x))
print("j at max: {}".format(y))

(Note: I have removed the HTML encoding for quotes in the code snippet.)

英文:

You are returning too early, this means you leave the function the first time that z>maxi is true, which means in the very first iteration.

Also, there are other mistakes that commenters pointed out:

  • maxi doesn't make much sense to print before calling the function
  • the for loop automatically increases the loop variables (or rather, iterates through the list that is range(1,5,1) and thus the incrementation doesn't actually do anything because it gets overwritten in the next step)

This is how it should look:

def i_j ():
    maxi = 0
    maxi_i = 0
    maxi_j = 0
    for i in range (1, 5, 1):
        for j in range (1, 5, 1):
            z = i+j
            print ("{}".format(z))
            if z > maxi:
                maxi = z
                maxi_i = i
                maxi_j = j
    return (maxi, maxi_i, maxi_j)

maxi, x, y = i_j ()
print ("Maximum: {}".format(maxi))
print ("i at max: {}".format(x))
print ("j at max: {}".format(y))

huangapple
  • 本文由 发表于 2023年4月17日 15:33:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定