python3 練習題100例 (五)
阿新 • • 發佈:2018-12-19
題目五:輸入三個整數x,y,z,請把這三個數由小到大輸出。
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 題目五:輸入三個整數x,y,z,請把這三個數由小到大輸出。""" __author__ = 'Fan Lijun' one = eval(input('請輸入三個數:')) two = eval(input('請輸入三個數:')) three = eval(input('請輸入三個數:')) #方法一:使用內建排序函式 lst = [one, two, three] print(lst.sort()) #方法二:我自己寫一個,鍛鍊一下if else lst2 = [] #獲得最小數 def minNumber(one, two, three): if one < two: if one < three: lst2.append(one) else: lst2.append(three) else: if two < three: lst2.append(two) else: lst2.append(three) def maxNumber(one, two, three): if one > two: if one > three: lst2.append(one) else: lst2.append(three) else: if two > three: lst2.append(two) else: lst2.append(three) minNumber(one, two, three) maxNumber(one, two, three) if lst2[0] == one: if lst2[1] == two: lst2.insert(1, three) else: lst2.insert(1, two) elif lst2[0] == two: if lst2[1] == three: lst2.insert(1, one) else: lst2.insert(1, three) else: if lst2[1] == two: lst2.insert(1, one) else: lst2.insert(1, three) print(lst2)