Suitable for beginners to learn Python from basic to advanced practice questions~ Arranged, come and act with me

1. Use the formatted output of the string to complete the display of the following business card

==========我的名片==========
姓名: itheima   
QQ:xxxxxxx
手机号:185xxxxxx
公司地址:北京市xxxx
===========================

2. Use the formatted output of the string to complete the display of the following business card

==========我的名片==========
姓名: itheima   
QQ:xxxxxxx
手机号:185xxxxxx
公司地址:北京市xxxx
===========================

3. Programming implementation: the user enters his name in the keyboard, such as "Zhang San", and the terminal prints "Hello, Zhang San"

Analysis of the answer to this question, hee hee, I don’t know if that’s the case, but what I understand is

# 这里让我们练习input方法 所以使用input接收用户输入即可
# 接收输入的名字
name = input("请输入自己的姓名:")
# 打印名字
print(name)

4. Determine whether the following code is written correctly, if not, please modify the code, and then execute the code. ,

int = 100
a = "200"
b = int(a)
print(b)

Answer analysis:

# 这里考察的是我们对变量的认知
# python中的内置方法是一个变量你可以对其赋值,但是赋值后就不能再次当方法使用了
# 修改只需要把第一行代码去掉即可

#int = 100  #错误 对内置方法进行了赋值,导致后面int方法不能使用
a = "200"
b = int(a)
print(b)

5. Write code to design a simple calculator that can perform basic addition, subtraction, multiplication and division operations.

Answer analysis:

# 这里考察的就是我们条件判断
# 我们现实中+-*/是让用户来选择的,那么我们根据用户的选择的操作 执行对应的行为
# 注意input 返回的都是字符串记得把数字转int类型
num1 = int(input("请输入第一个数字: "))
opt = input("请输入你要执行的操作(+ - * /): ")
num2 = int(input("请输入第二个个数字: "))

# 注意判断的时候是双等号
if opt=="+":
    # 使用f-string格式化,效果: 1 + 2 = 3
    print(f"{num1} {opt} {num2} = {num1+num2}")
elif opt=="-":
    print(f"{num1} {opt} {num2} = {num1-num2}")
elif opt=="*":
    print(f"{num1} {opt} {num2} = {num1*num2}")
elif opt=="/":   
    print(f"{num1} {opt} {num2} = {num1/num2}")
else:
    print("你要执行的操作无效!")

6. Questions about test scores: prompt the user to enter the score, determine which level it belongs to, and print the result to the console. A score below 60 fails, a score above 60 is a pass, a score of 70 to 80 is qualified, a score of 80 to 90 is good, and a score of 90 or more is excellent.

Answer analysis:

# 与上面的计算器类似 主要考察input 以及 多分支判断
# 从键盘获取分数,input 返回的是字符串记得转化成int
score = int(input("请输入你的成绩: "))
# 多分支判断成绩属于哪个档次
if score<60:
    print("不及格")
elif 60<=score<70:
    print("及格")
elif 70<=score<80:
    print("合格")
elif 80<=score<90:
    print("良好")
else:
    print("优秀")

7. Use a for loop to print each character in the string "abcdef" in turn.

Answer analysis:

# 考察for range的用法
pstr = "abcdef"
for s in pstr:
    print(s)

8. Code questions

[Code question]

Write the code according to the following requirements:

-Define the input_password function to prompt the user to enter the password

-If the user input length <8, throw an exception

-If the user input length>=8, return the entered password

 

Answer analysis:

# 无参数,返回用户校验后的面
def input_password():
    password = input("请输入密码: ")
    if len(password)<8:
        # 如果密码长度小于8位 抛出异常
        raise Exception("密码长度至少8位")
    else:
        return password
    
input_password()

 

Write a piece of code to fulfill the following requirements:

  1. Define a Person class with an initialization method in the class, and the name and age of the person in the method

  2. The name in the class is a public attribute, and age is a private attribute.

  3. Provide a public method get_age method for obtaining private attributes.

  4. Provide a method set_age method that can set private attributes. If the entered age is between 0 and 100, set the age. Otherwise, it prompts that the input is incorrect.

  5. When rewriting str requires printing objects, both the name and age are printed.

Answer analysis:

class Person():
    def __init__(self, name,age):
        # 创建的时候指定创建什么类型的水果
        self.name = name
        # 私有属性
        self.__age = age
        
    # 获取年龄,因为年龄是私有属性所以要提供对象的方法获取
    def Get_age(self):
        return self.__age
    # 设置年龄,因为年龄是私有属性所以要提供对象的方法进行修改
    def Set_age(self,age):
        self.__age = age
        
    # 重新__str__ 便于打印格式化
    def __str__(self):
        return f'name: {self.name}, age:{self.__age}'

# 测试代码
laowang = Person('laowang',50)
print(laowang)
laowang.Set_age(60)
print(laowang.Get_age())
print(laowang)

Okay, let’s practice here for the time being today. I need to practice more. If there is anything wrong, please help me to correct me. Thank you.

I’m a little girl, so please take a detour if you don’t like it or feel bad. Thank you~

Guess you like

Origin blog.csdn.net/weixin_45293202/article/details/114976369