Python日练习题目------六

  • 计算字符串中子串出现的次数
string = input("enter a string:")
str = input("enter a str:")
Count = string.count(str)
print(Count)
  • 请输入星期几的第一个字母来判断是星期几,如果第一个字母一样,则继续判断第二个字母
# 遍历字典里面的key,然后输出key对应的item。
week1 = {"m":"星期一", "w":"星期三", "f":"星期五"}
week2 = {"tu":"星期二", "th":"星期四", "sa":"星期六", "su":"星期日"}
Day_one = input("输入第一个字母:")
STR = ""

for KEY in week1.keys():
    if Day_one == KEY:
        print(week1[Day_one])
        break
    else:
        Day_two = input("输入第二个字母:")
        STR = Day_one + Day_two
        for KEYS in week2.keys():
            if STR == KEYS:
                print(week2[STR])
    break
  • 简单的猜拳,和电脑猜拳
import random
player = int(input("sss:"))
Com = random.randint(1, 100)
n = player - Com
if n > 0:
    print("电脑-%d:玩家-%d,玩家赢" % (Com, player))
elif n < 0:
    print("电脑-%d:玩家-%d,电脑赢" % (Com, player))
else:
    print("持平")
  • 钢琴落点
while True:
    i = 1
    for i in range(36):
        j = 0
        while j <= i:
            if i - j == 1:
                print("-\t", end="")
            else:
                print(" \t", end="")
            j += 1
        i += 1
        print()

    for i in range(37, 0, -1):
        j = 1
        while j <= i:
            if i - j == 1:
                print("-\t", end="")
            else:
                print(" \t", end="")
            j += 1
        print()
  • 从键盘依次输入各种字母,保存到磁盘文件中,知道输入o之后,程序停止
while True:
    so = input("enter:")

    if so == 'o':
        break
    else:
        with open("/Users/jason/Downloads/磁盘文件1.txt", "a", encoding="utf-8") as file:
            file.write(so)

        with open("/Users/jason/Downloads/磁盘文件1.txt", "r", encoding="utf-8") as File:
                content = File.read()
                print(content)
  • 随机产生各种字母,保存到磁盘文件中,知道输入o之后,程序停止
import string
import random
s = string.ascii_letters

for i in range(1000):
    so = random.choice(s)
    if so != 'o':
        with open("/Users/jason/Downloads/新的文件.txt", "a", encoding="utf-8") as file:
            file.write(so)

        with open("/Users/jason/Downloads/新的文件.txt", "r", encoding="utf-8") as File:
            content = File.read()
    else:
        break
print(content)
  • 从键盘呼入一个字符串,将小写字母全部换成大写字母,然后输出到一个磁盘文件”test”中保存,输入的字符串用!结束
little = input("enter:")
Big = little.upper()
with open("/Users/jason/Downloads/test.txt", "a", encoding="utf-8") as FIle:
    FIle.write(Big)
with open("/Users/jason/Downloads/test.txt", "r", encoding="utf-8") as FILE:
    content = FILE.read()
    print(content)
  • 有两个磁盘文件,A,B,各存放一行字母,要求把这两个文件中的信息合并,按照字母顺序排列,输出到一个新的文件夹中
# with open("/Users/jason/Downloads/A.txt", "a", encoding="utf-8") as Afile:
#     Afile.write("18岁生日在今朝")
#
# with open("/Users/jason/Downloads/B.txt", "a", encoding="utf-8") as Bfile:
#     Bfile.write("珍珠的心里真奇妙")

with open("/Users/jason/Downloads/A.txt", "r", encoding="utf-8") as afile:
    Aread = afile.read()
with open("/Users/jason/Downloads/B.txt", "r", encoding="utf-8") as bfile:
    Bread = bfile.read()
Cwrite = Aread + Bread
with open("/Users/jason/Downloads/C.txt", "w", encoding="utf-8") as Cfile:
    Cfile.write(Cwrite)

猜你喜欢

转载自blog.csdn.net/Jasonmes/article/details/80641207