Python练习题-005

题目-005:题目:输入三个整数x,y,z,请把这三个数由小到大输出

  • 分析:分析比较,先用x与y比较排序,然后再和后面的一次比较,确定x为最小;然后再后面确定y为最小,输出结果;
  • Python版本:Python 3.6.5

    代码1:基本代码,当然还是最经典的冒泡排序

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

x = int(input("please input the number x:"))
y = int(input("please input the number y:"))
z = int(input("please input the number z:"))
data = [x,y,z]
print("the number you input is:",data)
for i in range(0,3):
    for j in range(i+1,3):
        if data[i] > data[j]:
            data[i],data[j] = data[j],data[i]
print("the number Sort from small to large is:",data)
please input the number x:9
please input the number y:3
please input the number z:-5
the number you input is: [9, 3, -5]
the number Sort from small to large is: [-5, 3, 9]

    代码2:都说Python是很简洁的语言,当然也不需要那么多代码,一个sort函数就搞定了

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

x = int(input("please input the number x:"))
y = int(input("please input the number y:"))
z = int(input("please input the number z:"))
data = [x,y,z]
print("the number you input is:",data)
data.sort()
print("the number Sort from small to large is:",data)
please input the number x:6
please input the number y:8
please input the number z:9
the number you input is: [6, 8, 9]
the number Sort from small to large is: [6, 8, 9]
     代码3:例行拓展,不限定输入个数,封装后循环运行
#! usr/bin/python
#! -*- coding: utf-8 -*-

def SortNumber():
    data = []
    while True:
        try:
            input_A = int(input("please input the number:"))
            data +=[input_A]
        except:
            break
    print("the number you input is:",data)
    data.sort()
    print("the number Sort from small to large is:",data)
    print("End -*- End  "*5)

while True:
    SortNumber()
please input the number:9
please input the number:6
please input the number:3
please input the number:-4
please input the number:8
please input the number:100
please input the number:-99
please input the number:0
please input the number:
the number you input is: [9, 6, 3, -4, 8, 100, -99, 0]
the number Sort from small to large is: [-99, -4, 0, 3, 6, 8, 9, 100]
End -*- End  End -*- End  End -*- End  End -*- End  End -*- End  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值