输入和输出
一个Python程序可以从键盘读取数据,也可以从文件读取数据,而程序的结果可以输出到终端屏幕上,也可以保存到文件中便于以后使用。
Python用内置函数input()实现标准输入。
举例:
格式化输入和输出
练习
要求:
- 输入学生姓名;
- 依次输入学生的三门科目成绩;
- 计算该学生的平均成绩,并打印;
- 平均成绩保留一位小数;
- 计算该学生语文成绩占总成绩的百分比,并打印
注意:
注意:
a=input(“成绩”)
输入成绩输出结果是字符串,type(a) a的类型是str
不能算值,所以要转换 float()
a=float(input(" "))
注意强制类型转换 用python3.6
我写的:
student = str(input('please input student name:'))
math = float(input('please input math score:'))
chinese = float(input('please input chinese score:'))
English = float(input('please input English score:'))
sum = math + chinese + English
avg = sum / 3
print('%s的成绩是%.2f' %(student,avg))
scale = chinese / sum
print('比例是%.2f%%' %(scale * 100))
老师写的:
name = input("学生姓名:")
Chinese = float(input("语文成绩:"))
Math = float(input("数学成绩:"))
English = float(input("英语成绩:"))
#总成绩
sumScore = Chinese + Math + English
#平均成绩
avgScore = sumScore / 3
#百分比
ChinesePercent = ( Chinese / sumScore ) * 100
print("%s 的平均成绩为%.2f" %(name,avgScore))
print("语文成绩占总成绩的%.2f%%" %(ChinesePercent))
执行结果: