First code:
# -*- coding:gb2312 -*-
age=18
print("====================")
print("你的年龄是:%d"%age)
The execution result of the first code:
============Delimiter===========================
Second code:
# -*- coding:gb2312 -*-
age=18
print("====================")
print("你的年龄是:%s"%age)
The execution result of the second code:
============Delimiter===========================
The third code:
# -*- coding:gb2312 -*-
age=input("请输入您的年龄:")
print("您的年龄是:%d"%age)
Execution result of the third code:
============Delimiter===========================
Fourth code:
# -*- coding:gb2312 -*-
age=input("请输入您的年龄:")
print("您的年龄是:%s"%age)
Execution results of the fourth section of code:
Why in the first and second pieces of code, the execution result is the same whether you write %d or %s, but in the third and fourth pieces of code, whether you write %d or % s execution results are different? What's the problem and why is the third piece of code wrong? (PS: My python version is 3.5.1. The editor I use is Geany)
Python is unlike the C language. When declaring variables, you do not need to specify the type. It will automatically determine the type of the variable. All input from the keyboard is of string type and must be converted with int. Go check the variable assignment and you will understand the input and output.
Input input is a string by default, you need to use %s
You can use int() to convert it to a number, use %d