python - 根据给定条件组成一个数

标签 python list math

给定三个个位数,编写一个代码来找到中间数字最小的最大数字。例如,假设输入为 3, 9, 8,则生成的数字为 938。

输入格式

第一行包含第一个个位数,d1

下一行包含第二个个位数,d2

下一行包含第三个个位数,d3

输出格式

打印形成的数字

为什么我的代码是错误的?

d1=int(input())
d2=int(input())
d3=int(input())
lists=[d1,d2,d3]
y=lists.sort()
a=y[2]
b=y[1]
c=y[0]
num=(int(a)*100)+(int(c)*10)+int(b)
print(num)

最佳答案

.sort() 修改原始列表而不返回值,这里你想要的是这样的:

d1=int(input())
d2=int(input())
d3=int(input())
lists=[d1,d2,d3]
lists.sort()
a=lists[2]
b=lists[1]
c=lists[0]
num=(int(a)*100)+(int(c)*10)+int(b)
print(num)

关于python - 根据给定条件组成一个数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69572974/

相关文章:

python - 如何在不丢失重复值的情况下从两个列表创建字典?

C#数学图形库

python - 对列进行条件计数并对结果进行平均计算

python - 如何解决安装基于 pyproject.toml 的项目所需的错误 : Could not build wheels for hdbscan,

python - 具有 Python 相关信息的前五名表

linux - 迭代 cpu 事件运行队列列表 sched.h

python - Pandas:在 excel 文件中查找工作表列表

python - 将两个等长列表合并成一个字典,映射一对多

python - Sympy 有理数 - 表达 (x + 3 )**6/6

language-agnostic - 生成随机 6 个字符的字符串