1. 程式人生 > >python(練習實例)

python(練習實例)

ror tin 完全平方數 請問 mov utf pytho pro style

Python 練習實例1

題目:有四個數字:1、2、3、4,能組成多少個互不相同且無重復數字的三位數?各是多少?

我的代碼:python 3+

#2017-7-20

list_h = [1,2,3,4]
list_c = []
list_u = []
n = 0
for x in list_h:
    list_c = list_h[:]
    list_c.remove(x)
    for y in list_c:
        list_u = list_c[:]
        list_u.remove(y)
        for z in list_u:
            n 
+= 1 result = x * 100 + y * 10 + z print("第%d種:" % n , result)

推薦代碼:python 2+

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
for i in range(1,5):
    for j in range(1,5):
        for k in range(1,5):
            if( i != k ) and (i != j) and (j != k):
                print i,j,k


將for循環和if語句綜合成一句,直接打印出結果
#!/usr/bin/env python # -*- coding: UTF-8 -*- list_num = [1,2,3,4] list = [i*100 + j*10 + k for i in list_num for j in list_num for k in list_num if (j != i and k != j and k != i)] print (list)

Python 練習實例2

題目:企業發放的獎金根據利潤提成。利潤(I)低於或等於10萬元時,獎金可提10%;利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可提成7.5%;20萬到40萬之間時,高於20萬元的部分,可提成5%;40萬到60萬之間時高於40萬元的部分,可提成3%;60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高於100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤I,求應發放獎金總數?

#2017-7-20
# -*- coding:utf-8 -*-

profit = int(input("請輸入利潤值:"))
list_profit = [1000000,600000,400000,200000,100000,0]
point = [0.01, 0.015, 0.03, 0.05, 0.075 ,0.1]
bonus = 0
for x in range(len(point)):
    if profit > list_profit[x]:
        bonus += (profit - list_profit[x]) * point[x]
        profit = list_profit[x]
    else:
        continue
print(bonus)

Python 練習實例3

題目:一個整數,它加上100後是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少?

#2017-07-20
# -*- coding:utf-8 -*-

for x in range(2,85,2):
    y = 168 / x
    if x > y and (x + y) % 2 == 0 and (x - y) % 2 == 0:
        m = (x + y) / 2
        n = (x - y) / 2
        x = n * n - 100
        print(x)

Python 練習實例4

題目:輸入某年某月某日,判斷這一天是這一年的第幾天?

#2017-7-20
# -*- coding:utf-8 -*-

year = int(input("請輸入年份:"))
month = int(input("請輸入月份:"))
day = int(input("請輸入日:"))

if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0) :
    days = [0,31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30]
else:
    days = [0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]
which_day = 0
if month >= 1 and month <= 12:
    for x in range(month):
        which_day += days[x]
    which_day += day
    print("this day is the %dth day" % which_day)
else:
    print(error month type)

python(練習實例)