【问题标题】:Python compare two or more integer values and print out the higher one incl. the variable namePython 比较两个或多个整数值并打印出较高的值,包括。变量名
【发布时间】:2020-04-30 09:19:50
【问题描述】:

这一定是一个简单的问题,但我就是找不到正确的解决方案。

所以,我有 4 个变量。

number1 = 10
number2 = 14
number3 = 6
number4 = 11

我需要让程序获取所有变量,比较它们,然后打印出包括变量名在内的最高数字。 E.G

"variable 'number4' has the highest number with a value of: '11'

到目前为止,互联网上没有任何帮助,但我无法想象它有那么难。 我可能在这里错过了一个明显的功能或其他东西

【问题讨论】:

  • 使用max()获取最高值。正如herehere 所述,找到变量名似乎是不可能的。
  • 是的,但使用 max() 只会返回“4”。我还需要它来记住变量名。示例:“变量 w = 4 作为最大值”
  • 这能回答你的问题吗? Getting the name of a variable as a string

标签: python-3.x integer compare


【解决方案1】:

与其他问题相同:How to get max() to return variable names instead of values in Python?

基本上,将数据结构转换为字典而不是使用单个变量是有意义的。

【讨论】:

  • 就是这样..我找不到它。抱歉转帖
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-24
相关资源
最近更新 更多