本人于下学期该学习python,在听黑马程序员网课后,在此总结记录我的在课程学习后的习题练习。没有详细的解题过程,仅有代码和注释,如有错误希望大家多多指出。
目录
一,字符串格式化
题目如下:
答案如下:
name="传智博客"
stock_price=19.99
stock_code="003032" #不能写数字,股票代码以0开头
#股票 价格 每日 增长 因子
stock_price_daily_growth_factor=1.2
growth_days=7
finally_stock_price=stock_price*stock_price_daily_growth_factor**growth_days
print(f"公司:{name},股票代码:{stock_code},当前股价:{stock_price}")
print("每日增长系数:%.1f,经过%d天的增长后,股价达到了:%.2f"%(stock_price_daily_growth_factor,growth_days,finally_stock_price))
二,条件判断
01 if语句
题目如下:
答案如下:
#if语句,成年人买票判断
#键盘输入
age=int(input("请输入你的年龄:"))#int强制类型转换为整型
#if判断是否为成年人
if age>=18:
print("您已经成年,游玩需要买票10元")
print("祝您游玩愉快")
02 if else语句
题目如下:
答案如下:
#if else语句 我要买票吗
height=int(input("请输入你的身高(cm):"))
if height>120:
print("您的身高超出120,需要买票10元")
else:
print("您的身高低于120,可以免费游玩哦")
print("祝您游玩愉快")
03 if elif组合
题目如下:
答案如下:
#if elif组合猜想比较
num=5
if int(input("请猜猜一个数字:"))==num:
print("恭喜一次就猜对了呢")
elif int(input("猜错了,再猜一次:"))==num:
print("猜对了")
elif int(input("猜错了,再猜一次:"))==num:
print("恭喜,最后一次机会,你猜对了")
else:
print("sorry,猜错了")