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()
运行结果:
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)
运行结果:
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')
运行结果:
点赞好人,一生有钱