python - 如何在不使用循环的情况下找到(三个值)的最大值?[Python]
问题描述
我是编程新手,这让我磕磕绊绊,我正在考虑做这样的事情,但我无法更进一步
num1 = int(input('What is the first number?:'))
num2 = int(input('What is the second number?:'))
num3 = int(input('What is the third number?:'))
[[在此之后,我的脑海中正在考虑 elif 语句并使用 [and,or]]
解决方案
将所有内容添加variables
到 a中list
,然后您可以max
像这样使用该功能max(lista)
num1 = int(input('What is the first number?: '))
num2 = int(input('What is the second number?: '))
num3 = int(input('What is the third number?: '))
lista = [num1, num2, num3]
biggest = max(lista)
print(f"{biggest} is the largest value.")
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 max.py What is the first number?: 10 What is the second number?: 3 What is the third number?: 8 10 is the largest value.
只是为了一点点奖励,不包括处理TypeErrors
,但想给你一些想法,你可以在哪里使用这个小项目:
while True:
numbers = int(input("How many numbers would you like to enter: "))
values = []
for i in range(numbers):
if i == numbers - 1:
values.append(int(input(f"Enter 1 number: ")))
else:
values.append(int(input(f"Enter {numbers - i} numbers: ")))
print(f"\nThe largest number entered was {max(values)}")
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 max.py How many numbers would you like to enter: 5 Enter 5 numbers: 10 Enter 4 numbers: 8 Enter 3 numbers: 29 Enter 2 numbers: 13 Enter 1 number: 22 The largest number entered was 29
推荐阅读
- angular - 无法使用带有ngrx的角度读取未定义的属性“地图”
- java - AES/CBC/PKCS5Padding 加密与固定 IV(或没有)
- database - 有没有办法查询谷歌 ngrams 超过 10 个替换?
- javascript - 捕捉 Amcharts 的 CircleBullet 点击事件
- python - tkinter for 循环条目绑定连接到 sqlite3
- android - 为自定义构建类型(非调试)创建覆盖率报告不起作用
- matlab - 'cell' 类型的输入参数的未定义函数 'colon' - 单元格数组的问题
- selenium - 使用 Rust libtor 运行多个 Tor 实例
- c# - CodeWars 上的位计数 Kata
- docker - 管理 Docker 镜像卷