我的python代码上有一个奇怪的输出

时间:2018-01-17 15:49:27

标签: python output

我有一个代码,给我一个奇怪的输出。如果我打印(winst)它给出输出1234567891017,而变量" winst"实际上是17,所以数字12345678910来自哪里?以及如何解决它?

N = int(input())
Waardes = []
for i in range(N):
Waardes.append(int(input(str(i + 1))))

Minimum = 100000000
Maximum = 0
Bezit = False
Winst = 0
for i in range(N):
if (Waardes[i] < Minimum):
    Minimum = Waardes[i]
    Bezit = True
elif (Waardes[i] > Maximum):
    Maximum = Waardes[i]
    if (Bezit):
        if (N > (i + 1)):

            if (not Waardes[i + 1] > Maximum):
                Winst = Winst + (Maximum - Minimum)
                Minimum = Maximum
                Maximum = 0
                Bezit = False
        else:
            Winst = Winst + (Maximum - Minimum)
            Minimum = Maximum
            Maximum = 0
            Bezit = False

print(Winst)

1 个答案:

答案 0 :(得分:0)

它来自这一行:

Waardes.append(int(input(str(i + 1))))

我想它应该缩进。

Help on built-in function input in module __builtin__:

input(...)
    input([prompt]) -> value

    Equivalent to eval(raw_input(prompt)).

没有必要提示。

N = int(input())
Waardes = []
for i in range(N):
    Waardes.append(int(input()))

Minimum = 100000000
Maximum = 0
Bezit = False
Winst = 0
for i in range(N):
  if (Waardes[i] < Minimum):
    Minimum = Waardes[i]
    Bezit = True
  elif (Waardes[i] > Maximum):
    Maximum = Waardes[i]
    if (Bezit):
        if (N > (i + 1)):

            if (not Waardes[i + 1] > Maximum):
                Winst = Winst + (Maximum - Minimum)
                Minimum = Maximum
                Maximum = 0
                Bezit = False
        else:
            Winst = Winst + (Maximum - Minimum)
            Minimum = Maximum
            Maximum = 0
            Bezit = False

print(Winst)