Basic Python programming code exercises (5)

1. Define an anonymous function to find the product of two numbers

The implementation code is as follows:

def chengfa(num1,num2):
    result=num1*num2
    print(result)

a = float(input('请输入一个数:'))
b = float(input('请再输入一个数:'))
chengfa(a,b)

operation result:

 

2. Define an anonymous function, the parameter is a dictionary, and return the value whose key is age in the dictionary

The implementation code is as follows:

func2 = lambda x: x.get('age')
func3 = lambda x: x['age']

my_dict = {'name': '李白', 'age': 22}
print(func2(my_dict))
print(func3(my_dict))

operation result:

 

3. Ticket issue

A scenic spot charges tickets at different prices according to the age of tourists ( the ticket price is 20 yuan , free for minors under 18 years old, and free for seniors over 60 years old ) . Please write the tourist class, determine the ticket price that can be purchased according to the age group and output

Note, exit the program when you enter "n"

tourists

Name

age

Show name and ticket price

 

The implementation code is as follows:

flag = 1
while flag:
    name  = input("请输入姓名:")
    if name == "n" or name == "N":
        flag = 0
        print("退出程序")
        break
    else:
        age = int(input("请输入年龄:"))
        if 0 < age < 18 or 60 < age < 150:
            print(name,"的年龄是",age,"门票价格是免费")
        elif 18 <= age <= 60:
            print(name, "的年龄是", age, "门票价格是20元")
        else:
            print("年龄输入错误")

operation result:

 

Fourth, define the administrator class

  1. Implementation ideas
    1. Define the administrator class Administrator
    2. define its properties and methods

The implementation code is as follows:

class Administrator():
    def __init__(self, account, password):
        self.account = account
        self.password = password
    def show(self):
        print("账户名是:",self.account,"密码是:",self.password)

5. Define the customer class

  1. Define the customer class Customer
  2. Define properties and methods
  3. write client class
    1. Attributes: points, card type

Method show() : display customer information (display points, card type)

The implementation code is as follows:

class Customer():
    def __init__(self, integral, cardType):
        self.integral = integral
        self.cardType = cardType
    def show(self):
        print("积分卡积分是:",self.integral,"卡的类型是:",self.cardType)

6. Create an administrator object

  1. Implementation ideas
    1. Create two objects of admin class
    2. Assign values ​​to two objects and call

     display method

 The implementation code is as follows:

class CLanguage :

    name1 = "admin1"
    password1 = "111111"
    name2 = "admin2"
    password2 = "222222"
def __init__(self,name1,password1,name2,password2):
        #下面定义 2 个实例变量
    self.name1 = name1
    self.password1 = password1
    self.name2 = name2
    self.password2 = password2
    print('姓名:',name1,"密码:",password1)
    print('姓名:',name2,"密码:",password2)

Seven, change the administrator password

  1. Implementation ideas
    1. Loop execution
    2. Create an object of the administrator class

 The implementation code is as follows:

class Administrator():
    def __init__(self, name, password):
        self.name = name
        self.password = password

    def show(self):
        print("姓名是:",self.name,"密码是:",self.password)



admin = Administrator("admin","123456")



name = input("请输入姓名:")
pwd = input("请输入密码:")
if admin.name == name and admin.password == pwd:
    newPwd = input("请输入新密码")
    admin.password = newPwd
    print("修改密码成功,您的新密码是:",newPwd)
else:
    print("用户名和密码不匹配!您没有权限更新管理员信息")

 

 

Guess you like

Origin blog.csdn.net/qq_63010259/article/details/130612336