python3 和 python2 input 功能的不同点

python3 和 python2 input的区别:

  • python3会把输入的值当前一个字符串解析
  • python2 被表当前变量来执行

列子

#-*- coding:utf-8 -*-

name = input('你的名字是?')
age = input('你的年龄是?')
qq = input('你的qq是?')

print("======================")
print ('我的名字是%s'%name)
print ('我的年龄是%s'%age)
print ('我的QQ是%s'%qq)
print("======================")

python3运行效果

file

python2运行效果

file

如果在python2中不想输入的值会被当成变量解析.使用raw_input()即可

#-*- coding:utf-8 -*-

name = raw_input('你的名字是?')
age = raw_input('你的年龄是?')
qq = raw_input('你的qq是?')

print("======================")
print ('我的名字是%s'%name)
print ('我的年龄是%s'%age)
print ('我的QQ是%s'%qq)
print("======================")

代码示例:

file

运行效果:

file

猜你喜欢

转载自blog.csdn.net/cpongo9/article/details/89251026