首页 > 文章列表 > python输入三个数字从小到大排序

python输入三个数字从小到大排序

python输入
165 2022-08-07

1、说明

使用list.sort()的方法排列,首先要保证用户输入的数字必须是整数,如果不是整数,则需要提示用户输入错误并重新输入。使用try函数,提示Valueerror的错误。

2、实例

# -*- coding: utf-8 -*-
while True:
    try:
        x = int(input('请输入第一个整数,按回车键继续:'))
        y = int(input('请输入第二个整数,按回车键继续:'))
        z = int(input('请输入第三个整数,按回车键继续:'))
    except ValueError:
        print ('输入错误,请重新输入')
        continue
    list = [x,y,z]
    list.sort()
    print (list)

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。