《练习100》31~35

题目31

# press any key to change color,do you want to try it. Please hurry up!

# 第一种使用颜色配置输出
# 向终端输出彩色字符,色彩的设置由目标终端文字系统和转义字符控制,与具体的编程语言无关
# 参数名称	参数值
# 文字效果	0:终端默认,1:高亮,4:下划线,5:闪烁,7:反白显示
# 前景色	30(黑),31(红),32(绿),33(黄),34(蓝),35(紫),36(青),37(灰)
# 背景色	40(黑),41(红),42(绿),43(黄),44(蓝),45(紫),46(青),47(灰)
# 举例说明:
# print('\033[文字效果;前景色;背景色m文字内容\033[0m')
# print('\033[0;32mclortest\033[0m')
# #
# print('\033[31mclortest\033[0m')
# 为什么结尾处要有个'\033[0m',这是为了不影响后面的终端输出文字颜色
from colorama import init, Fore, Back, Style
import os
import msvcrt


def color_change():
    color = [f'\033[{x}m' for x in range(30, 38)]
    color_end = '\033[0m'
    index = 0
    while True:
        # 这个东西用了,目前不知道怎么退出程序(kill -9 可以强行停止)
        # os.system('pause')
        print("请按任意键继续. . .")
        msvcrt.getch()
        if index == len(color):
            index = 0
        print(f'{color[index]}Hello World!!!{color_end}')
        index += 1

# color_change()

# index = 0
# while True:
#     str1 =


# 第二种使用python的colorama模块,该模块的使用方法这里不详细讲述
# Fore是针对字体颜色,Back是针对字体背景颜色,Style是针对字体格式
# Colorma在使用时需要及时关闭colorma的作用范围,否则后面所有的输出都会是指定的颜色
def color_change1():
    # 定义颜色的范围
    color = ["BLACK", "RED", "GREEN", "YELLOW",
             'BLUE', 'MAGENTA', "CYAN", "WHITE"]
    color_fore = [Fore.BLACK, Fore.RED, Fore.GREEN, Fore.YELLOW,
                  Fore.BLUE, Fore.MAGENTA, Fore.CYAN, Fore.WHITE]
    index = 0
    init(autoreset=True)  # 初始化,并且设置颜色设置自动恢复,这样就不用手动结束颜色作用范围了
    while True:

        if index == len(color_fore):
            index = 0
        os.system('pause')
        print(color_fore[index] + color[index])
        # print(color_fore[index] + color[index] + Fore.RESET) #没有使用init就要用这个
        index += 1


color_change1()

# 任意键继续,无法通过ctrl + c等手段停止程序,停止请先查看程序进程号,然后使用kill -9 进程号 停止程序运行

题目32

# 学习gotoxy()与clrscr()
# 了解到的gotoxy() 是移动光标的函数,clrscr()是清屏
# 没有看到有关的这两个函数的博文,然后有的话也是收费的。
# 先说gotoxy()移动光标,先搜搜有关光标的模块,找到两个pyautogui和pynput

import os
import pyautogui
def getxy():
    return pyautogui.position()

def gotoxy(x:int, y:int):
    pyautogui.moveTo(x, y)
    

def autoMove1Step():
    """自动获取当前的光标位置,然后下移动1
    停止运行的话,通过kill -9 进程号 的方式
    """
    while True:
        point = ()
        point = getxy()
        print(point)
        gotoxy(point[0], point[1]+1)
        
# autoMove1Step()

# clrscr()清屏,这个更简单,os模块提供了system()用于执行命令,查看一下windows下的清屏命令为cls linux 为clear
def clrscr():
    os.system('cls')
    # os.system('clear')

print('t11111estse')
clrscr()

# 在网上看到另外一种清屏方式通过ANSI转义序列
# '\033[2J'表示清除整个屏幕,'\0331;1H'表示将光标移动到屏幕左上角
import sys
def clrscr1():
    sys.stdout.write("\033[2J\033[1;1H")
    sys.stdout.flush()
print('testse')
clrscr1()

题目33

# 练习函数调用

def  func_test(arg1, arg2, *args, arg3 = 0):
    print(f'{arg1}, {arg2}, {args}, {arg3}')
    
# 有默认值的函数的调用方式
func_test(1,2,[3,4,5,6])
func_test(1,2,[3,4,5,6],arg3 = 7)

# 关键字参数: 在函数调用的时候指定形参的名字
func_test(1,2,[3,4,5,6],arg3 = 7)

# 位置参数:按位置传递参数
func_test(1,2,[3,4,5,6])

# 带*的参数
# 给*参数传参的时候在实参前面加*标识解压,将实参里面的元素堪称一个一个的实参,依次放入args中
# 如果给带*的形参传值的时候,不在实参前加*,标识把该实参看作是一个整体传递给args
# 以下函数的执行结果中,可以看到两种调用方式的区别。
func_test(1,2,[3,4,5,6])
func_test(1,2,*[3,4,5,6])

# 带**的形参,在调用时实参要求是字典


# 限制调用时的实参传值方式: / 仅限位置参数, *仅限关键字参数
# / 前的形参,在调用时仅限位置参数
# * 之后的形参,在调用时仅限关键字参数
def limit_call(arg1, arg2, /):
    print(f'{arg1}---{arg2}')
    
limit_call(1,2)
#limit_call(arg1 = 2, arg2=3) # limit_call() got some positional-only arguments passed as keyword arguments: 'arg1, arg2'
def limit_call1(*, arg1, arg2):
    print(f'{arg1}---{arg2}')

# limit_call(1,2) # limit_call() takes 0 positional arguments but 2 were given
limit_call1(arg1 = 2, arg2=3)

# 若/和*的作用区域重叠  / must be ahead of *
# def limit_call1(*, arg1, arg2,/):
#     print(f'{arg1}---{arg2}')


def limit_call2(arg1,/, arg2, *, arg3):
    print(f'{arg1}---{arg2}---{arg3}')

limit_call2(123, arg2 = "hello", arg3 = "world")
limit_call2(123, "hello", arg3 = "world")

题目34

# 求100以内的素数
# 素数: 在大于1的自然数中除了1和它自己之外没有别的因数
# 假设该数为n,那么只要他与[2,n-1]这个范围内的任意一个数的余数为0,说明他不是素数
def is_prime_number(num:int):
    if num <=1:
        return False
    for i in range(2,num):
        if not num %i:
            return False
    return True

for i in range(1,101):
    if is_prime_number(i):
        print(f"{i} ", end='')
print()

题目35

# 文本颜色设置

# 利用termcolor库中的colored
from termcolor import colored


def write_to_file(name: str, content: str, color:str):
    with open(name, mode='w') as f:
        print(colored(content,color))
        # black, red, green, yellow, blue, magenta, cyan, white, light_grey, dark_grey, 
        # light_red, light_green, light_yellow, light_blue, light_magenta, light_cyan.
        
write_to_file('text.txt','hello world', 'red')

你可能感兴趣的:(Python学习笔记,python,开发语言,学习)