show you the code
ans = input("最近学习了吗?yes/no: ")
while ans == 'yes':
print("very good\n1.Python\n2.高数\n3.退出")
selection = input("请输入学习选项: ")
if selection == '1':
print("Python真不戳!")
elif selection == '2':
print("高数真难啊...")
elif selection == '3':
print("已退出")
break
else:
print("选项有误,请重新输入")
show you the code
2.1 while输出1-10
i = 1
while i <= 10:
print(i, end=' ')
i += 1
2.2 水仙花数
def flower_calculate(n):
zxc = []
while n != 0:
zxc.append(n%10)
n //= 10
return sum(map(lambda x: x**3, zxc))
i = 100
while i < 1000:
if i == flower_calculate(i):
print(i)
i += 1
2.3 分别打印1-100的奇数和偶数
zxc = [i for i in range(1, 101)]
print("偶数:", zxc[1::2])
print("奇数:", zxc[::2])
2.4 求和
print(sum([i if i&1 else -i for i in range(1, 100)]))
2.5 判断闰年
year = int(input("请输入年份:"))
if year % 4 == 0:
if year % 100 == 0:
print("%d 是闰年" % year)
elif year % 25 == 0:
print("%d 不是闰年" % year)
else:
print("%d 是闰年" % year)
else:
print("%d 不是闰年" % year)
2.6 猜数字
import random as rd
init = rd.randint(1, 100)
print("现在,随机生成了一个数,这个数在[1, 100]内")
while True:
guess = int(input("猜猜看:"))
if guess == init:
print("猜对了!游戏结束")
break
elif guess < init:
print("小了,再", end='')
else:
print("大了,再", end='')