用户交互input

input() 函数
接收到的都是str,如果输入为数字,打印结果想进行运算,此时需要转义.
语法:
内容=input("提示信息")
这里可以直接获取到用户输入的内容.

a = input("请输入你的名字:")
print (type (a) ) #打印一下 a 的类型, 显示 class 'str'
print ("My name is "+a) 或者写成 print ("My name is ",a) 是一样的结果
假如用户输入的为数字,以上执行时数字不会进行计算,只是进行连接,,此时需要转义
例如
a = input("请输入一个数字:")
b = input ("请在输入一个数字:")
c = int(a)
d = int(b)
print (c + d)
也可以写成
a = int ( input ("请输入一个数字:"))
b = int ( input ("请再输入一个数字:"))
print ( type(a),type(b) )
print ( a + b )

猜你喜欢

转载自www.cnblogs.com/xiaozhangpython/p/9580762.html
今日推荐