How to input three numbers in python and output the largest value

青灯夜游
Release: 2023-01-05 16:08:26
Original
84428 people have browsed it

Method: First use the input() function to receive the three numbers input from the keyboard; then use the float() or int() function to uniformly convert the three received values ​​into floating point numbers or integers; then use " if else" statement compares the sizes of three numbers to obtain the maximum number; finally use the print() function to output the maximum number.

How to input three numbers in python and output the largest value

The operating environment of this tutorial: windows7 system, Python3, Dell G3 computer.

In python, you can use the input() function to input three numbers, and use the float() (or int()) function, print() function and "if else" statement to find the maximum value and output it. .

Implementation idea:

  • The input() function receives three numbers input from the keyboard;

  • float() or int() function uniformly converts the three received values ​​into floating point numbers or integers;

  • The "if else" statement compares the sizes of the three numbers to obtain the maximum Number;

  • #print() function outputs the maximum number.

Implementation code: input three numbers and output the largest number among the three numbers

# 第一种写法:
num1 = float(input('请输入第一个数:'))
num2 = float(input('请输入第二个数:'))
num3 = float(input('请输入第三个数:'))
if num1 < num3 and num2 < num3:
    big_num = num3
elif num1 < num2 and num3 < num2:
    big_num = num2
else:
    big_num = num1
print(&#39;三个数中最大数为:%s&#39; % big_num)


# 第二种写法(工作量会指数级增加):
n1= int(input(&#39;please enter the firest number:&#39;))
n2 = int(input(&#39;please enter the second number:&#39;))
n3 = int(input(&#39;please enter the third number:&#39;))
max_num = 0

if n1 > n2:
    max_num = n1
    if n1 > n3:
        max_num = n1
    else:
        max_num = n3
else:
    max_num = n2
    if n2 > n3:
        max_num = n2
    else:
        max_num = n3
print(&#39;the max_num is:%d&#39;%max_num)


# 第三种写法(最简写法):
num1 = float(input(&#39;请输入第一个数:&#39;))
num2 = float(input(&#39;请输入第二个数:&#39;))
num3 = float(input(&#39;请输入第三个数:&#39;))
max_num = num1        # 先假设num1最大
if max_num < num2:
    max_num = num2
if max_num < num3:
    max_num = num3
print(&#39;最大数是:%f&#39; % max_num)
Copy after login

Related function description

The input() function in Python3.x accepts a standard input data and returns it as string type.

int() function is used to convert a string or number into an integer.

float() function is used to convert integers and strings into floating point numbers.

【Related recommendations: Python3 video tutorial

The above is the detailed content of How to input three numbers in python and output the largest value. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!