【问题标题】:how to use variebles in list[variable]如何在列表[变量]中使用变量
【发布时间】:2017-12-31 13:14:37
【问题描述】:
import sys
sort = []   #list
def myMax(mylist):   # function instead of max()
    listadd = 0
    plus1 = listadd + 1
    for listadd in range(a):
    if sort[listadd] > sort[plus1, a]: # where i get error
        return sort[listadd]
while True:
try:
    a = int(input(" How many numbers do you want to compare: "))
    break
except ValueError:
    sys.stderr.write('ERROR\n')
    sys.stderr.write(' Try Again... \n')
for i in range (a):
    while True:
        try:
            n = int(input("Please enter a number: "))
            sort.append(n)
            break
        except ValueError:
            sys.stderr.write('ERROR\n')
            sys.stderr.write(' Try Again... \n')
print(myMax(sort), " is the biggest number! ") # function instead of max()
SystemExit()

简单的最大程序错误。

程序找到最大值。一些数字没有使用max() 并安装所需的模块。

你如何制作一个做同样事情的函数?

【问题讨论】:

  • 你的代码不是有效的python。
  • python 3 是哪一个。 2
  • 它给出了一个错误,这就是我要问的如何解决它
  • 考虑到这个问题的质量,我们能给出的最佳答案是“修复错误”。
  • @timgeb 这个问题应该去掉python标签,因为它不是python!

标签: python python-3.x python-3.2


【解决方案1】:

您需要遍历列表中的每一项,检查它是否大于目前为止最高的一项。

x=lst[0]
for i in lst:
    if i > x:
        x = i
return x

【讨论】:

  • 或者不要。如果 OP 明确地不努力回答问题,你为什么要努力回答?
  • 我是python初学者
  • @timgeb 很公平,但我喜欢认为自己是善良的?
  • 我重命名了您的列表变量(名称list 已被内置列表占用)。
  • @timgeb 谢谢,我没想到!
猜你喜欢
  • 2021-02-21
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 2018-01-11
相关资源
最近更新 更多