Files
2026-07-22 16:42:56 +08:00

53 lines
1.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 第 1-3 课示例:输入与输出
#
# 这个程序会暂停并等待用户依次输入四项数据。
# 输入数字时请只输入数字本身,不要添加“天”或“小时”等单位。
# 一、接收字符串输入
# input() 会先显示提示语,再等待用户输入并按下回车键。
# 用户输入的内容会以字符串形式保存到 user_name 变量中。
user_name = input("请输入你的名字或称呼:")
# 二、接收整数输入
# input() 得到的 learning_days_text 是字符串。
# 这里分成两步编写,方便观察类型转换前后的数据。
learning_days_text = input("请输入你已经学习 Python 的天数:")
learning_days = int(learning_days_text)
# 三、接收浮点数输入
# 每天学习时间可能包含小数,因此使用 float() 进行转换。
daily_hours_text = input("请输入你计划每天学习的小时数:")
daily_hours = float(daily_hours_text)
# 四、继续接收字符串输入
learning_goal = input("请输入你的 Python 学习目标:")
# 五、处理输入的数据
# 一周有 7 天,因此每天学习时间乘以 7 可以得到每周计划时间。
weekly_hours = daily_hours * 7
# 六、输出分隔线
# print() 接收三个字符串sep="" 表示它们之间不添加默认空格。
print("=", "个人 Python 学习计划", "=", sep="")
# 七、使用 f-string 输出变量和计算结果
print(f"学习者:{user_name}")
print(f"已学习:{learning_days}")
print(f"每天计划学习:{daily_hours} 小时")
print(f"每周计划学习:{weekly_hours} 小时")
print(f"学习目标:{learning_goal}")
# 八、演示 end 参数
# 第一条 print() 使用 end="",输出后不换行,而是以中文冒号结尾。
# 第二条 print() 会紧接着输出学习状态,之后恢复默认换行。
print("当前状态", end="")
print("持续学习中")