How to change a for loop into a while loop in Python?

CSDN Q & A 2022-05-14 15:50:19 阅读数:773

changelooplooppython

How to change the following code into while loop ?

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))




Take the answer :
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))

If help , Please accept !


版权声明:本文为[CSDN Q & A]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/134/202205072313140000.html