循环和列表 [英] Loop and the list
问题描述
大家好
有一个大问题。这是我的代码:
----- BEGIN
#stale programowe
G = 6.67E-11
Mz = 6E + 24
Rz = 6.37E + 6
TX =范围(10,780,2)
TY = []
def skoknatezenia(ilpkt):
" Oblicza skok"
dr =((2 * Rz) -Rz)/ ilpkt
返回博士
def natezeniep(TX,TY,dr):
" Liczy natezenie w przedziale< Rz,2 * Rz>"
cnt = 0
for i in range(0,2 * Rz,dr):
TY.append((G * Mz)/((Rz + cnt)*(Rz + cnt)))
cnt = cnt + dr
返回TY
def maxvalue(TY):#gets TY列表中最大值的索引
max = 0
for i在范围(0,len(TY)):
if(max< TY [i]):
max = i
打印TY [i],max
继续
返回最大值
def normowanie(TY):#normalize TY列表项
for i in range(0,len(TY)-1):
TY [i] = TY [i] / float( TY [0])#例如''0',应该是''max''
返回TY
Koniec = False
而Koniec<>是的:
尝试:
Koniec2 = False
而Koniec2<>是的:
ilpkt = raw_input(Podaj ilosc punktow do wykreslenia 200):\ t")
尝试:
int(ilpkt )
ilpkt = int(ilpkt)
Koniec2 = True
除外:
print" Ilosc punktow musi byc liczba calkowita!\t"
继续
Koniec = True
除外:
print" Nieoczekiwany BLAD。 Sprobuj jeszcze raz。
dr = skoknatezenia(ilpkt)
TY = natezeniep(TX,TY,dr)
打印TY #first main point
max = maxvalue(TY)
print" max =",max
TY = normowanie(TY )#normalize函数
打印TY
-----结束
我不明白为什么函数maxvalue返回9。当我启动一个
程序并检查第一个主要点中的TY列表时,最大值有一个索引
0''。然后将列表放入函数maxvalue,给我索引
''9''。 ; - /
错误在哪里?
ps
为波兰语文本道歉,在代码中。我会回答任何问题。
-
Krzysztof Szynter:''''''。 :。 :*
Dygi GG 1027078:......''..... ::: ..... 。 。 。 。 .....
http://newbie.friko.pl :: :.... ::::: .... :: ::: .. :: ....
dygimail(at)poczta(dot)fm:...'': ....:'':::::: ..'':: ....
>
Krzysztof Szynter写道:def maxvalue(TY):#gets一个TY列表中最大值的索引
max = 0
for i in range(0,len(TY)):
if(max< TY [i]):
max = i
打印TY [i],max
继续
返回最大值
max是一个索引,对吗?所以你的测试应该是
如果TY [max]< TY [i]:
[....]
在您的代码中,您将索引与值进行比较。
>
问候,
Holger
Krzysztof Szynter< dy ****** **@SPAMpoczta.fm>写在
新闻:Xn ****************************** @ 127.0.0.1:
[...]我不明白为什么函数maxvalue返回9。当我启动一个程序并在''第一个主要点''中检查TY列表时,最大值有一个索引0''。然后将列表放入函数
maxvalue,给我索引9。 ; - /
错误在哪里?
ps
在代码中为波兰语文本道歉。我会回答
任何问题。
在maxvalue中,您将索引与值进行比较,这似乎是
不太可能是你想要的。你的意思是:
if(TY [max]< TY [i]):
-
rzed
Krzysztof Szynter写道:大家好
有一个大问题。这是我的代码:
[snipped]
如果我理解正确,你只需要找到最大值。如果它是
所以,有更好的方法:内置最大功能:
max([1 ,2,3])
问候,
anton。
Hi all
Have got a big problem. This is my code:
-----BEGIN
#stale programowe
G = 6.67E-11
Mz = 6E+24
Rz = 6.37E+6
TX = range(10,780,2)
TY = []
def skoknatezenia(ilpkt):
"Oblicza skok"
dr = ((2*Rz)-Rz)/ilpkt
return dr
def natezeniep(TX,TY,dr):
"Liczy natezenie w przedziale <Rz,2*Rz>"
cnt = 0
for i in range(0,2*Rz,dr):
TY.append((G*Mz)/((Rz+cnt)*(Rz+cnt)))
cnt = cnt + dr
return TY
def maxvalue(TY): #gets an index of the max value in the TY list
max = 0
for i in range(0,len(TY)):
if (max<TY[i]):
max = i
print TY[i],max
continue
return max
def normowanie(TY): #normalize the TY list items
for i in range(0,len(TY)-1):
TY[i] = TY[i]/float(TY[0]) #for example ''0'', should be ''max''
return TY
Koniec = False
while Koniec <> True:
try:
Koniec2 = False
while Koniec2 <> True:
ilpkt = raw_input("Podaj ilosc punktow do wykreslenia 200):\t")
try:
int(ilpkt)
ilpkt = int(ilpkt)
Koniec2 = True
except:
print "Ilosc punktow musi byc liczba calkowita!\t"
continue
Koniec = True
except:
print "Nieoczekiwany blad. Sprobuj jeszcze raz."
dr = skoknatezenia(ilpkt)
TY = natezeniep(TX,TY,dr)
print TY #first main point
max = maxvalue(TY)
print "max =",max
TY = normowanie(TY) #normalize the function
print TY
-----END
I don''t understand why function maxvalue returns "9". When i start a
program and check TY list in ''first main point'', the max value has an index
0''. Then putting the list into the function maxvalue, gives me the index
''9''. ;--/
Where is the mistake?
ps
Apologise for the Polish text''s, in the code. I''ll answer for any question.
--
Krzysztof Szynter :''''''. :. : *
Dygi GG 1027078 :...'' ..... : : : ..... . . . . . .....
http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :....
dygimail(at)poczta(dot)fm :...'' :.... : '': :.... : : :..'' : :....解决方案
Krzysztof Szynter wrote:def maxvalue(TY): #gets an index of the max value in the TY list
max = 0
for i in range(0,len(TY)):
if (max<TY[i]):
max = i
print TY[i],max
continue
return max
max is an index, right? So your test should be
if TY[max] < TY[i]:
[....]
In your code, you compare the index to a value.
Greetings,
Holger
Krzysztof Szynter <dy********@SPAMpoczta.fm> wrote in
news:Xn******************************@127.0.0.1:
[...]I don''t understand why function maxvalue returns "9". When i
start a program and check TY list in ''first main point'', the max
value has an index 0''. Then putting the list into the function
maxvalue, gives me the index ''9''. ;--/
Where is the mistake?
ps
Apologise for the Polish text''s, in the code. I''ll answer for
any question.
In maxvalue, you are comparing an index to a value, which seems
unlikely to be what you want. Did you mean:
if (TY[max] < TY[i]):
--
rzed
Krzysztof Szynter wrote:Hi all
Have got a big problem. This is my code:
[snipped]
If I understand you correctly, you just need to find maximum. If it''s
so, there is better way: built-in max function:
max([1, 2, 3])
3
regards,
anton.
这篇关于循环和列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!