print ('welcome to the new world')
print('what is your name')
myName = input()
print ('it is good to meet you , ' + myName)
print (' Th length of your name is : ')
print (len(myName))
print('what is your age')
myEdge = input()
print ('you were born on ,')
print (2018 - myEdge)
上面的代码在最后一行失败了。
追踪(最近一次呼叫最后一次):
File "C:\Users\Pratik\Desktop\python\First_Program.py", line 10, in <module>
print (2018 - myEdge)
TypeError: unsupported operand type(s) for -: 'int' and 'str'
但我可以通过赋值来手动运行它,并且在我们将变量数据类型转换为字符串print(2018 - int(myEdge))
时它正在运行混淆了为什么脚本和命令行执行之间存在差异
myEdge = 29 打印(2018年 - myEdge) 1989
答案 0 :(得分:0)
在线命令:
myEdge = 29 # You put an integer in the variable
在剧本中:
myEdge = input("Enter your age -> ") # You put a string in the variable
这就是为什么你在行命令和脚本之间有区别。
您有脚本int(myEdge)
的解决方案。有关提示,您可以在输入中添加文本:input("add_the_text_here")
。
此外,在行命令中,您可以测试相同的内容:
>>> t = input(">")
>29
>>> type(t)
<class 'str'>
>>> 30 - t
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
30 - t
TypeError: unsupported operand type(s) for -: 'int' and 'str'