python built-in function input / eval (22)

python fact a lot of built-in functions, and wherein the input eval considered special, built-in functions interactive input belongs, eval function can be performed directly string expression and returns the value of the expression.

 

A function .input

Python's built-in function input is also an interactive function, what is the interactive function? Interactive programs are programs that can take user interaction.

Possible previous code, some children's shoes may feel somewhat rigid, variable declarations and definitions have been prepared in advance, maybe old driver will say you do not run the program and I know what the result of the output of yes.

six

input () function can receive user input, and returns the string str type , the following sample code:

1

2

3

while True:

    A = INPUT ( "Please enter:" )

    Print ( "content of the input is: {} type: {}" . the format ( A , type ( A ) ) )

input

 

We now know that AI is the trend, the next decade belongs to AI, so today I want to tell you the value of a million AI core code :( Sand Sculpture robot version 1.0)

Tell you a little secret

1

2

3

4

5

6

7

whileTrue:

    '''

    replace ( "?", "!") The question mark in a string English state? Replaced with an exclamation point!

    replace ( "?", "!") The question mark in a string Chinese state? Replaced with an exclamation point!

    replace("吗","") 将字符串中的中文 "吗" 替换为 ""

    '''

    print(input("").replace("?","!").replace("?","!").replace("吗",""))

测试结果:

1

2

3

4

5

6

7

8

9

10

在吗?

!

你好

你好

python教程吗?

python教程!

看完教程就能学会编程吗?

看完教程就能学会编程!

python能生孩子吗

python能生孩子

 

二.eval函数

在以前的文章中我们也曾经使用过eval,将字符串转为eval 内置函数eval()除了这个功能还能直接执行字符串表达式,并返回表达式的结果,示例代码如下:

1

2

3

4

5

6

7

8

9

a = eval("3+2.5")

print("a={} ,a的类型是{}".format(a,type(a)))

 

a = eval("3+2")

print("a={} ,a的类型是{}".format(a,type(a)))

 

b = 33

a = eval("b/2")

print("a={} ,a的类型是{}".format(a,type(a)))

输出结果:

1

2

3

a=5.5 a的类型是<class 'float'>

a=5 a的类型是<class 'int'>

a=16.5 a的类型是<class 'float'>

 

三.input函数和eval函数配合使用

input函数和eval函数配合使用完成一个***面的计算器:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

# !usr/bin/env python

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

"""

@Author:何以解忧

@Blog(个人博客地址): shuopython.com

@WeChat Official Account(微信公众号):猿说python

@Github:www.github.com

@File:python_input_eval.py

@Time:2019/10/6 21:48

 

@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

"""

 

while True:

    result = eval(input("请输入表达式:"))

    Print ( "result:" , the Result )

Output:

1

2

3

4

5

6

7

Please enter the expression: 3 + 2 - 6

Results: - 1

Please enter the expression: 55 * 25

Results: 1375

Please enter the expression: 89 / 7

Results: 12.714285714285714

Enter the expression:

 

IV. Key summary

1. Note that the return value is input string str type, or list int type if desired can be accomplished by converting

2.eval is a very powerful built-in functions, in addition to the above functions, eval can delete system files, do not do too much to explain here, do not pay attention to eval abuse.

 

you may also like:

1.python return logical expression

2.python anonymous function lambda

3.python list comprehensions

4.python dictionary derivations

 

Reproduced please specify : ape say Python  »  Python built-in function input / eval


Guess you like

Origin blog.51cto.com/14531342/2459815