python中for循环怎么改成while循环?

CSDN问答 2022-05-14 15:38:09 阅读数:860

Python循环怎么改改成

以下代码怎么改成while循环?

def maxnum(lst):
maxn = 0
for i in lst:
if maxn < i:
maxn = i
return maxn
def minnum(lst):
minn = lst[0]
for i in lst:
if minn > i:
minn= i
return minn
a = [3,4,6,7]
print(maxnum(a) - minnum(a))




采纳答案:
def maxnum(lst): maxn = 0 i = 0 while i<len(lst): if maxn < lst[i]: maxn = lst[i] i+=1 return maxndef minnum(lst): minn = lst[0] i = 0 while i<len(lst): if minn > lst[i]: minn= lst[i] i += 1 return minna = [3,4,6,7]print(maxnum(a) - minnum(a))

如有帮助,请采纳!


版权声明:本文为[CSDN问答]所创,转载请带上原文链接,感谢。 https://ask.csdn.net/questions/7711919