首页 > 解决方案 > 如何在不使用循环的情况下找到(三个值)的最大值?[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]]

标签: python

解决方案


将所有内容添加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

推荐阅读