【Python】【demo实验5】【练习实例】【排序】

原题:

输入三个整数x,y,z,请把这三个数由小到大输出。

我的解法:

#!/usr/bin/python
# encoding=utf-8
# -*- coding: UTF-8 -*-

# 输入三个整数x,y,z,请把这三个数由小到大输出。
l = []
l.append(int(input("first num:\n")))
l.append(int(input("second num:\n")))
l.append(int(input("third num:\n")))

"""
l[0] = int(input("first num:\n"))
l[1] = int(input("second num:\n"))
l[2] = int(input("third num:\n"))
"""

if l[0] > l[1]:
    l[0],l[1] = l[1],l[0]
    if l[1] > l[2]:
        l[1],l[2] = l[2],l[1]
        if l[0] > l[2]:
            l[0],l[2] = l[2],l[0]

            
for i in range(len(l)):
    print(l[i],end = " ")
    if i == len(l)-1:
        break
    else:
        print("",end = " < ")
    

输出的结果:

C:\Python30_demo>python 020demo.py
first num:
6
second num:
4
third num:
5
4  < 5  < 6

原文给出的解答:

————————(我是分割线)————————

参考:

1. RUNOOB.COM  : https://www.runoob.com/python/python-exercise-example5.html

备注:

初次编辑时间:2019年9月23日23:29:02

环境:Windows 7   / Python 3.7.2

猜你喜欢

转载自www.cnblogs.com/kaixin2018/p/11575658.html