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])
breakelse:
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 = 1for i in range(36):
j = 0while j <= i:
if i - j == 1:
print("-\t", end="")
else:
print(" \t", end="")
j += 1
i += 1print()
for i in range(37, 0, -1):
j = 1while j <= i:
if i - j == 1:
print("-\t", end="")
else:
print(" \t", end="")
j += 1print()
从键盘依次输入各种字母,保存到磁盘文件中,知道输入o之后,程序停止
while True:
so = input("enter:")
if so == 'o':
break
else:
withopen("/Users/jason/Downloads/磁盘文件1.txt", "a", encoding="utf-8") asfile:
file.write(so)
withopen("/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':
withopen("/Users/jason/Downloads/新的文件.txt", "a", encoding="utf-8") asfile:
file.write(so)
withopen("/Users/jason/Downloads/新的文件.txt", "r", encoding="utf-8") as File:
content = File.read()
else:
break
print(content)