python小题入门练习(不断更新...)

目录

1.循环输出0~10

2.循环输出2,10

3.输出2,5,8,11

4.输出50个*

5.求1~50的累加和

6.用户尽可以登录三次

7.投骰子

8.打印4*4金星

9.打印三角形金星

10.打印逆置三角形金星

11.截取某个字母

12.倒着截取某个字母

13.获取字符串长度

14.截取字符串某一段

15.跳跃截取字符串

16.倒着截取某段字符串

17.字符串逆序

18.随机生成验证码

19.带参函数调用

20.图书借阅函数

21.文件读取操作

22.判断文件是否可读

23.读取文件一行

24.循环读取文件

25.读取文件

26.写文件

27.把一句话写到文件中

28.异常处理机制

29.n的阶乘

30.学生成绩排序




1.循环输出0~10

for i in range(11):
    print(i)

2.循环输出2,10

for i in range(2,11):
    print(i)

3.输出2,5,8,11

for i in range(2,12,3):
    print(i)

4.输出50个*

print('*'*50)

5.求1~50的累加和

sum=0
for i in range(1,51):
    sum+=i
print(sum)

6.用户尽可以登录三次

for i in range(3):
    username = input('用户名:')
    password = input('密码:')
    # 判断输入是否正确
    if username == 'admin' and password == '123456':
        print('用户登录成功')
        break
    print('用户名或密码有误!\n')
else:
    print('用户被锁定')

7.投骰子

规则:

掷骰子
两个:1-6
1.玩游戏要有金币,否则不能玩有系
2.玩游戏赠金币1个,可以充值金币
3.10元的倍数,20个金币
4.玩游戏消耗5个金币
5.猜大小 猜对了给两个金币   猜错了没有奖励  超出六点以上认为是大,否则是小
6.游戏结束:1.主动退出 2.没有金币
7.只要退出则打印金币,共玩了几局

import random

# 金币数
coins = 0
# 计数器
count = 0
if coins < 5:
    # 提示充值
    print('金币不足请充值再玩')
    while True:
        money = int(input('请输入充值金额:'))
        if money % 10 == 0:
            coins += money//10*20
            print('充值成功,当前金币有%d个' % coins)
            # 游戏开始
            print('~~~~~~~~~~游戏开始~~~~~~~~~')
            answer = input('是否开启游戏(y/n):')
            while coins > 5 and answer == 'y':

                # 扣金币
                coins -= 5
                # 赠金币
                coins += 1
                # 投骰子
                ran1 = random.randint(1, 6)
                ran2 = random.randint(1, 6)
                # 猜大小
                guess = input('请输入猜测结果:')
                # 判断比较
                if guess == '大' and ran1 + ran2 > 6 or guess == '小' and ran1 + ran2 <= 6:
                    print('恭喜你赢了')
                    coins += 2
                else:
                    print('很遗憾')
                answer = input('是否继续玩(y/n):')
                # 玩的次数
                count += 1

            print('共玩了%d次,金币数为%d' % (count, coins))
            break

        else:
            print('不是10的倍数,充值失败!')

8.打印4*4金星

n = 1
while n <= 4:
    print('****')
    n += 1

9.打印三角形金星

n = 1
while n <= 5:
    print('*'*n)
    n += 1

10.打印逆置三角形金星

m = 1
while m <= 5:
    print('*'*(6-m))
    m += 1

11.截取某个字母

s1 = 'ABCDEF'
print(s1[4])

12.倒着截取某个字母

s1 = 'ABCDEF'
print(s1[-1])

13.获取字符串长度

s1 = 'ABCDEF'
print(len(s1))

14.截取字符串某一段

s1 = 'ABCDEF'
print(s1[0: 3])

15.跳跃截取字符串

s1 = 'ABCDEF'
print(s1[0: 6: 2])

16.倒着截取某段字符串

s1 = 'ABCDEF'
print(s1[-2: -1])

17.字符串逆序

s1 = 'ABCDEF'
print(s1[::-1])

18.随机生成验证码

import random

def generate_code():
    s = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    code = ''
    for i in range(4):
        r = random.choice(s)
        code += r
    print(code)

generate_code()

19.带参函数调用

def get_sums(a, b):
    if isinstance(a, int) and isinstance(b, int):
        s = a + b
        print(s)
    else:
        print('类型错误!')

get_sums(2, '4')

20.图书借阅函数

def borrow_book(bookname, number = 1):
    print('进入借书系统')
    print('要借阅的书名是:{},借阅的数量:{}'.format(bookname, number))

borrow_book('狂人日记')

borrow_book('草房子', 3)

21.文件读取操作

stream = open('d:/lab1.txt')
contains = stream.read()
print(contains)

22.判断文件是否可读

stream = open('d:/lab1.txt')
result = stream.readable()
print(result)

23.读取文件一行

stream = open('d:/lab1.txt')
line = stream.readline()
print(line)

24.循环读取文件

stream = open('d:/lab1.txt')
while True:
    line = stream.readline()
    print(line)
    if not line:
        break

25.读取文件

stream = open('d:/lab1.txt')
lines = stream.readlines() # 读取到列表中
for i in lines:
    print(i)

26.写文件

stream = open('d:/lab1.txt', 'w')
s = '''
你好我的世界
'''
stream.write(s)
stream.close()

27.把一句话写到文件中

stream = open('d:/lab1.txt', 'w')
stream.write('你好我的大学')
stream.close()

28.异常处理机制

def func():
    try:
        s1 = int(input('输入第一个数字:'))
        s2 = int(input('输入第二个数字:'))
        sum = s1 + s2

        print('和', sum)
    except:
        print('请输入数字')
func()

29.n的阶乘

n = int(input())
ns = 1
for i in range(1, n+1):
    ns = ns*i
print(ns)

30.学生成绩排序

if __name__ == '__main__':
    students = [
        {"sno": 101, "sname": "小张", "sgrade": 88},
        {"sno": 102, "sname": "小王", "sgrade": 99},
        {"sno": 103, "sname": "小李", "sgrade": 77},
        {"sno": 104, "sname": "小赵", "sgrade": 66},
    ]

    students_sort = sorted(students, key=lambda x: x["sgrade"], reverse=True)

    print(f"source {students} , sort result:{students_sort}")

31.括号生成

class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        if n == 1:
            return list({'()'})
        res = set()
        for i in self.generateParenthesis(n - 1):
            for j in range(len(i) + 2):
                res.add(i[0:j] + '()' + i[j:])
        return list(res)

你可能感兴趣的:(python,python,开发语言,后端)