# 循环三要素:
# 1.初始化数据
# 2.循环的条件
# 3.在循环过程中,如何改变循环条件
i = 1
while i <= 100:
print(i)
i = i + 1
从键盘获取一个路径,判断此目录是否存在,不存在则创建,存在则提示
import os
dirPath = input("请您输入您需要检测的路径:")
if os.path.exists(dirPath) :
pass
else:
os.makedirs(dirPath)
print("创建成功")
输入一个年份,判断是否是闰年
# 1.输入一个年份 input()
year = input("请您输入一个年份:")
# 2.上述输入的年份是一个字符串,需要转换为int
year = int(year)
# 3.判断是否是闰年:1.能被4整除,并且不能被100整除
# 2.能被400整除
if year % 4 == 0 and year % 100 != 0:
print("此数是闰年")
elif year % 400 == 0:
print("次数是闰年")
else:
print("此数非闰年")
求1-100的和
sum = 0
i = 1
while i <= 100:
sum = sum + i
i = i + 1
print("1-100 的和:{}".format(sum))
0-100的质数的输出
i = 1
while i <= 100:
isZhi = True
x = 2
while x <= i - 1:
if i % x == 0:
isZhi = False
break
x = x + 1
if isZhi == True:
print("{}是质数".format(i))
i = i + 1
99乘法口诀表
# 一共9行 *
# n行有n列 *
# 列 * 行 = 结果
#每一行结束换行 ,每一个表达式结束以后则空\t
row = 1
while row <= 9:
col = 1
while col <= row:
print("{} * {} = {} ".format(col,row,col*row),end='\t')
col = col + 1
print()
row = row + 1
猜拳游戏
import random
while True:
print("0剪刀 1石头 2布 q退出")
# 玩家出拳
choose = input("请您出拳:")
if choose == 'q':
print("感谢您的使用,欢迎下次再来!")
break
if choose not in ['0','1','2']:
print("您输入有误,请检查\n")
continue
# 电脑出拳
computer = str(random.randint(0,2))
# 玩家展示
playerChoose = ""
if choose == "0":
playerChoose = "✄"
elif choose == "1":
playerChoose = "☭"
else:
playerChoose = "❁"
# 电脑展示
computerChoose = ""
if computer == "0":
computerChoose = "✄"
elif computer == "1":
computerChoose = "☭"
else:
computerChoose = "❁"
print("玩家: {} VS :{} 电脑".format(playerChoose,computerChoose))
if (choose == "0" and computer == "2") or (choose == "1" and computer == "0") or (choose == "2" and computer == "1"):
print("玩家胜!")
elif(choose == str(computer)):
print("平局")
else:
print("电脑胜")
print()
从键盘获取一个数字n,获取其1-n之间所有不包含7的数字,并打印出来
num = input("请您输入一个数字:")
num = int(num)
i = 1
while i <= num:
if "7" not in str(i):
print(i)
i += 1
统计字符串中大小写字母的数量
str = "jskdhLKSAFLKFlksdjflkasjf"
bigNum = 0
smallNum = 0
i = 0
while i < len(str):
if str[i].isupper():
bigNum += 1
else:
smallNum += 1
i += 1
print("大写字母的数量:{}".format(bigNum))
print("小写字母的数量:{}".format(smallNum))