作业day04
1. 使用while循环输出1 2 3 4 5 6 8 9 10
i = 0
while i < 10:
i += 1
if i == 7:
continue
print(i)
2. 求1-100的所有数的和
s = 0
for i in range(101):
s = s+i
print(s)
3. 输出 1-100 内的所有奇数
for i in range(1, 100, 2):
print(i)
4. 输出 1-100 内的所有偶数
for i in range(2,101,2):
print(i,end=" ")
5. 求1-2+3-4+5 ... 99的所有数的和
s = 0
for i in range(1,100):
if i % 2 == 1:
s += i
elif i % 2 == 0:
s -= i
print(s)
6. 用户登陆(三次机会重试)
username = "lic"
password = 123456
for i in range(3):
a = input("请输入用户名:")
b = int(input("请输入密码:"))
if a == username and b == password:
print("登录成功!")
break
else:
print("请输入正确的用户名与密码!!!")
7:猜年龄游戏
要求:
允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
import random
true_age = random.randint(1,100)
for i in range(3):
num1 = int(input("请输入你猜测的年龄:"))
if num1>true_age:
print("猜大了!")
elif num1 == true_age:
print("恭喜你猜对了!")
break
elif num1< true_age:
print("猜小了!")
8:猜年龄游戏升级版(选做)
要求:
允许用户最多尝试3次
每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
如何猜对了,就直接退出
import random
true_age = random.randint(1,100)
num1 = 0
while (num1 != true_age):
for i in range(3):
num1 = int(input("请输入你猜测的年龄:"))
if num1>true_age:
print("猜大了!")
elif num1 == true_age:
print("恭喜你猜对了!")
break
elif num1< true_age:
print("猜小了!")
answer = input("你还想继续玩吗?")
if answer == "Y" or answer == "y":
continue
elif answer == "N" or answer == "n":
break
9.for循环打印99乘法表
for i in range(1, 10):
for j in range(1, 10):
if i > j:
continue
print(f'{i}*{j}={i * j}', end=" ")
print('\t\n')
10.for循环打印金字塔:如下
*
***
*****
*******
*********
for i in range(1, 10, 2):
s = f"{''i}"
print(s.center(9, ' '))