Q1: 输入姑娘的年龄后,进行以下判断:
- 如果姑娘小于18岁,打印“未成年”
- 如果姑娘大于18岁小于25岁,打印“表白”
- 如果姑娘大于25岁小于45岁,打印“阿姨好”
- 如果姑娘大于45岁,打印“奶奶好”
age=int(input("请输入姑娘的年龄: "))
if 0<age<18:
print("未成年")
elif age<25:
print("表白")
elif age<45:
print("阿姨好")
else:
print("奶奶好")
Q2:# 打印1-100之间的偶数
lst=[]
for i in range(1,101):
if i%2==0:
lst.append(i)
print(lst)
Q3:猜年龄游戏升级版,有以下三点要求:
- 允许用户最多尝试3次
- 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
answer = 25 # 设定猜测的年龄
max_attempts = 3 # 每轮最多尝试次数
continue_game = True # 控制是否继续游戏的变量
while continue_game:
attempts = 0 # 记录已尝试次数
while attempts < max_attempts:
guess = int(input("请输入猜测的年龄:"))
if guess == answer:
print("恭喜,猜对了!")
break
else:
attempts += 1
if attempts < max_attempts:
print(f"猜错了,你还有 {max_attempts - attempts} 次机会。")
else:
choice = input("很遗憾,你已经尝试了三次,还想继续玩吗?(Y/N): ")
if choice.lower() == 'n':
continue_game = False
elif choice.lower() == 'y':
continue_game = True
attempts = 0
else:
print("无效的输入,游戏结束。")
continue_game = False
Q4:打印如下所示9*9乘法表
for i in range(1,10):
for j in range(1,i+1):
print(f"{i}*{j}={i*j}",end=" ")
print()
Q6:# 打印如下所示金字塔:
max_level=5
* current_level=1,空格数=4,*号数=1
*** current_level=2,空格数=3,*号数=3
***** current_level=3,空格数=2,*号数=5
******* current_level=4,空格数=1,*号数=7
********* current_level=5,空格数=0,*号数=9
数学表达式
空格数 = max_level-current_level
*号数 = 2*current_level-1
max_level=5
for current_level in range(1,max_level+1):
print(f"{' '*(max_level-current_level)}{'*'*(2*current_level-1)}{' '*(max_level-current_level)}")