Chapter 1-1 Input two numbers from the keyboard, calculate their sum and output

topic description

insert image description here

answer

Using python to do the first question stumped me. The code I wrote at the beginning was like this:

a = input()
b = input()
print(a+b)
'''
output:
1
2
12
'''

obviously it's not right

Usage of input function

At the beginning, I was still complacent, python does not need to specify the type of the variable when inputting. However, when we directly assign values ​​to variables, python will automatically determine the type of stored data , and we do not need to operate, but this is not the case for the input function.
Regardless of whether the value we input is int, float or string, the type returned by the input() function is string . Therefore, strings cannot perform arithmetic operations. If you want to use input data to perform mathematical operations, you need to specify the type of data in advance. (i.e. perform a type conversion)

Correct spelling of this question:

a = int(input())
b = int(input())
print(a+b)

input() function with prompt

example:

name = input('请输入你的名字')
print(name)
'''
output:
请输入你的名字lmy
lmy
'''
name = 'lmy'
number = input('你好!'+str(name)+'请输入你的学号: ')
print(number)
'''
output:
你好!lmy请输入你的学号: 202005
202005
'''

Guess you like

Origin blog.csdn.net/qq_52109814/article/details/121856357
Recommended