The use of python's input () function-enter the desired value in the terminal, and Xiaobai can learn the way of python

table of Contents

Foreword

One, input primary use

Second, the advanced use of input: with if else


Foreword

Starting from the long python road, as a programmer, I have to write the front end, do the back end, write the sql, and understand the deployment. Of course, the recent hot python must also be engaged, so I have to toss up. Come, learn java with me on the left, learn python from Xiaobai together on the right, learn together, grow together 

 

One, input primary use

Today I learned the usage of input () function, which is a function to input a string in the terminal, that is, after the code is run, the user enters the specified value on the computer.

For example, I prompted on my computer: Which one do you like, Andy Lau and Wu Yanzu?

Input: Wu Yanzu

Output: Wu Yanzu, I like you

First we assign values ​​to the result of the input () function, then use the input () function to collect information, and finally use the print () function to output the result

demo

name = input('刘德华和吴彦祖你喜欢哪一个:')
print(name+',我喜欢你')

Running effect diagram:

 

Second, the advanced use of input: with if else

Here's an advanced, use input and if else together

Code 1:

print('你选择你最喜欢的明星:1:刘德虎 2:吴彦祖')
choice = input('请输入您的选择:')
#变量赋值

if choice == '1':
#条件判断:条件1
    print('刘德华,我喜欢你')
#条件1的结果

else:
#条件判断:其他条件
    print('吴彦祖,我喜欢你')
#其他条件的结果

Output result:

 

 Code 2:

print('你选择你最喜欢的明星:1:刘德虎 2:吴彦祖')
choice = input('请输入您的选择:')
#变量赋值

if choice == 1:
#条件判断:条件1
    print('刘德华,我喜欢你')
#条件1的结果

else:
#条件判断:其他条件
    print('吴彦祖,我喜欢你')
#其他条件的结果

operation result:

Also input is 1 in the terminal, but due to different codes, one is the string 1 and the other is the integer 1, so the running result is different,

The reason is: the input value of the input () function will always be [mandatory] converted to the [string] type. (Fixed rules for Python3)

 We use the type () function to verify

temp = input('请输入1或2:')
print(type(temp))

Although the input value obtained in the terminal is a string, but we can add an int () function to the input () function and force it to be an integer type, which can be changed to other types we want

temp = int(input('请输入1或2:'))
print(type(temp))

Generally forced, we will use it in age, money or number 123 electives,

For example, the following example

money = int(input('你一个月工资多少钱?'))
#将输入的工资数(字符串),强制转换为整数

if money >= 10000:
#当工资数(整数)大于等于10000(整数)时
    print('好有钱吖,借我一点呗')
#打印if条件下的结果

elif 5000 < money < 10000:
#当工资数(整数)大于5000(整数)小于10000(整数)时
    print('你的钱也还行')
#打印elif条件下的结果

else:
#当工资数(整数)小于等于5000(整数)时
    print('回家养猪去咯')
#打印else条件下的结果

 

Published 88 original articles · Liked 151 · Visits 450,000+

Guess you like

Origin blog.csdn.net/qq_27471405/article/details/103996443