天天看點

七夕寫代碼【Python】練習題

1.

功能說明:

百分制成績轉換為等級制成績。

要求:如果輸入的成績在90分以上(含90分)輸出A;80分-90分(不含90分)輸出B;70分-80分(不含80分)輸出C;60分-70分(不含70分)輸出D;60分以下輸出E。

核心代碼:

while(True):
    a =int(input('Please enter grade: ')) 
    if 0 <= a < 60:
        print('Grade E')
    elif a<70:
        print('Grade D')
    elif a<80:
        print('Grade C')
    elif a<90:
        print('Grade B')
    elif a<=100:
        print('Grade A')
    else:
        print('error!!')
        quit()
           

運作結果:

七夕寫代碼【Python】練習題

2.

功能說明:

寫一段python代碼,模拟使用者登入過程

該使用模拟使用者登入系統的過程,分為幾個部分,每個部分用一個函數實作:

1、擷取使用者的輸入

2、校驗使用者的輸入是否合法,簡單設定校驗規則,比如長度限制在10個字元以内,可以自己定。

3、校驗使用者是否是已知使用者,并且使用了正确的密碼登入

4、記錄線上使用者數

核心代碼:

a = {'zhang':'666666','li':'55555'}#已知使用者賬号密碼
def inp():
    n = input('pleace input name: ')
    return n

def test(n):
    if len(n)>10:
        print('input error!!')
        quit()

def password(n):
    k=0
    for i in range(len(a)):
        if a.get(n):
            m = input('Existing account,please input password:')
            if m ==a[n]:
                print('The password is correct,Login succeeded!!!')
                k=2
                break
            else:
                print('The password is error!!!')
                quit()
    if k==0:
        print('new user,Login succeeded!!!')
    return 1

def number(s,num):
    if s == 1:
        num  = num + 1
    return num
 
u = 0
while(True):
    n=inp()
    test(n)
    s = password(n)
    u = number(s,u)
    print('Number of online users',u)
           

運作結果:

七夕寫代碼【Python】練習題

3.

功能說明:

寫一個超市收銀的功能,即根據購物清單和産品單價,計算出最終收銀的結果。

要求使用python的類和方法進行實作。

需求:

1. 最基本的收銀操作,根據客人購買的商品單價和數量來計算所購商品的價格

2. 七夕促銷,超市搞活動,所有商品打77折

3. 中秋和國慶節促銷,現在對中秋節商品做滿減活動,滿300減30,國慶節購物滿1000有十分之一的機率免單

4. 又到了一年七夕節,今年的活動和去年不同,力度更大,除了77折以外,滿99元随機附送禮品一份。隻需要修改去年七夕活動的類就可以了

展現面向對象程式設計在應對需求變化的時候,代碼的可複用性。

提示:利用繼承的方式,定義不同的類實作各個不同活動業務功能

核心代碼:

import random
class qixi:#七夕
    def qiqi(price):
        price = price * 0.77
        return price

class Mid_autumn:#中秋
    def mid(price):
        if price >= 300 :
            price = price - 30
        return price

class National:#國慶
    def national(price):
        if price >=1000 :
            if random.randint(1, 10)==1:
                price = 0
                print('Over 1000 free of charge!')
            else:
                print("Didn't win the prize!")
        return price

class nextqixi(qixi):
    def nqiqi(price):
        if price >=99:
            print('Congratulations on getting a gift!')

def Price():
    c = 0
    price = 0
    while(c == 0):
        p = int(input('Unit price:'))
        q = int(input('quantity of goods:'))
        price = price + p * q
        c  = int(input('To finish, press 1. To continue, press 0: '))
    return price

ch = int(input('七夕請按1,中秋請按2,國慶請按3,又一年七夕請按4:'))
price = Price()
if ch == 1:
    price = qixi.qiqi(price)
elif ch == 2: 
    price = Mid_autumn.mid(price)
elif ch == 3: 
    price = National.national(price)
elif ch == 4: 
    nextqixi.nqiqi(price)
    price = nextqixi.qiqi(price)
print('Still need to pay:  ',price,'yuan')
           

運作結果:

七夕寫代碼【Python】練習題
七夕寫代碼【Python】練習題

點贊好人,一生有錢