文章目录
1.input基本概念
variable=input() 表示运行后,可以在屏幕中输入一个数字,该数字会赋值给自变量。看代码:
get_input = input('please input a number:',)
print('this is a:',get_input)
#输出
please input a number:100
this is a: 100
input()应用在if语句中.
在下面代码中,需要将input() 定义成整型,因为在if语句中自变量 get_input 对应的是1 and 2 整数型。输入的内容和判断句中对应的内容格式应该一致。
也可以将if语句中的1and2 定义成字符串,其中区别请读者自定写入代码查看。
get_input = int(input('please input a number:')) #input返回的是字符串形式,要把它转化为整数形式
if get_input == 1:
print('This is one')
elif get_input == 2:
print('This is two')
else:
print('not one or two')
#输出
please input a number:3 #输入3
not one or two
2.input拓展
用input()来判断成绩
score = int(input('please input your score:'))
if score >= 90:
print('you got A')
elif score >= 80:
print('you got B')
elif score >= 70:
print('you got C')
elif score >= 60:
print('you got D')
else:
print('not pass exam')
#输出
please input your score:59 #输入59
not pass exam