【问题标题】:assigning variables in a list based on its value in relation to the other values in the list根据变量相对于列表中其他值的值在列表中分配变量
【发布时间】:2017-01-18 01:11:29
【问题描述】:

该函数以正整数n为参数,生成200到625之间的3个随机数,最小的随机数称为minValue,中间的随机数称为myTaret,最大的随机数为称为 maxValue。

def usingFunctionsGreater(n):
#create random numbers
aList=[]
for i in range(3):
    aList.append(random.randrange(200,625,1))
    #assign minValue, myTarget, and maxValue

cmets 应该有助于解释我要编写的程序,但我不知道如何将变量分配给生成的列表中的元素。

【问题讨论】:

  • n在这里的作用是什么?
  • 你不能按升序对aList 进行排序吗?

标签: python list random variable-assignment


【解决方案1】:

以下内容如何:

>>> min, target, max = sorted([random.randrange(100, 625, 1) for i in range(3)])
>>> min, target, max
(155, 181, 239)
>>> 

【讨论】:

  • 非常感谢您的帮助。
  • @J.Wade 如果这解决了您的问题,而不是(或除此之外)说“谢谢”,您可以将此答案标记为解决方案。你得到代表,他得到代表,每个人都是赢家。
猜你喜欢
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多