Automation input operation and maintenance [python] ------ python in [input (), getpass module, raw_input ()] and the output [Print ()]

1. The receiving user data entered from the keyboard

We can not just write programs and programmers to interact, be sure to interact with the user, such as receivingInformation entered by the user, the information processing, and finally a feedback to the user

1.1python3 in

1.1.1 normal input echo input ()

Generally, we let the user input, we can echo the content.

>>> input('Num:')
Num:2
'2'
>>> input('Num:')
Num:abc
'abc'

All content of our inputEchoing a string

1.1.2 password is not echo (getpass module)

In python we have a lot of third-party modules, here we usegetpass module. canUser interaction and does not echo

For example, we have a 123456 password, the password will be assigned to the variable num.

>>> import getpass
>>> num = getpass.getpass('请输入密码:')
请输入密码:

Then you enter the content will not show up, you canPrint num variableTo obtain a password value:

>>> print(num)
123456

1.2 in python2

1.2.1 bool available digital input ()

To which inputNumber 2

>>> input('num:')
num:2
2

When we enter a number, it isNumeric display

To which inputString 'songyr'

>>> input('num:')
num:songyr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'songyr' is not defined

Enter a stringAn errorA.

Note : In the python2input () function only support numeric type and the correct type bool

EntryFloat

>>> input('num:')
num:1.2
1.2

Entrybool type

>>> input('num:')
num:True
True

>>> input('num:')
num:False
False

1.2.2 string using the raw_input ()

If you must enter a string, this timeUse raq_input () function

>>> raw_input('num:')
num:2
'2'

>>> raw_input('num:')
num:songyr
'songyr'

This time and python3 the input () function is the same,Inputs have become a string

1.3 python3 receive data comparing

Python3 received in the string of numbers, we canDirect conversion compared to numeric

>>> num = int(input('num:'))
num:123
>>> print(num)
123

This can be directlyNumeric compare

2. Output print ()

To print () direct input variable so that it canChange the variable output change

2.1 Integer

2.1.1 Direct output integer

  • % D: integer
>>> name = 'songyanru'
>>> age = 21
>>> print('%s的年龄是%d' %(name,age))
songyanru的年龄是21

This time we directly change the value of variables can change the output.

2.1.2 fixed-length integer

But also through provisionConventional bit and change bits to determine

  • For example, we have a total of eight student number 0317xxxx student number, we can use == '0317% .4d' == When writing, you can determine the eight student number.
>>> name = 'song'
>>> num = 1
>>> print('%s的学号是0317%.4d' %(name,num))
song的学号是03170001

2.2 String

>>> name = 'liuyaowen'
>>> age = 14
>>> print('%s的年龄是%d' %(name,age))
liuyaowen的年龄是14

Formatted output:

  • % S: string placeholder

2.3 Float

2.3.1 direct output float

  • % F: float
>>> name = '工资'
>>> money = 12.113
>>> print('%s是%f' %(name,money))
工资是12.113000

Note :% F 0 will automatically fill Reserved 6 decimal places

2.3.2 fixed length decimal

weUse Reserved% .nf n decimals

>>> name = '工资'
>>> money = 12.113
>>> print('%s是%.3f' %(name,money))
工资是12.113

2.3.3 percentage

  • First of all10% is 0.10. First output 0.10
>>> num = 0.1
>>> print('%.2f' %(num))
0.10
  • will0.10 turns 10
>>> print('%.2f' %(num * 100))
10.00

thenPlus% on it

>>> print('%.2f%' %(num * 100))     
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: incomplete format

Note : This mustUse %% can add a%

>>> print('%.2f%%' %(num * 100))
10.00%

3. Exercises

Averaging scores (python3 interpreter):

  • Enter the name of the student.
  • Followed by three subjects enter student achievement ( 'language, mathematics, English').
  • Clearing change students' grade point average, and print.
  • Grade point average to one decimal place.
  • Language performance calculated as a percentage of the total score.

Overall result

name = input('输入你的姓名:')
yu = int(input('输入你的语文成绩:'))
shu = int(input('输入你的数学成绩:'))
ying = int(input('输入你的英语成绩'))
print('%s的总成绩是%d' % (name, yu + shu + ying))

The average score

print('语文成绩所占百分比是%.2f%%' %( yu / (yu + shu + ying) * 100))

result

输入你的姓名:song
输入你的语文成绩:12
输入你的数学成绩:80
输入你的英语成绩56
song的总成绩是148
语文成绩所占百分比是8.11%

CanDefine multiple variables

zong = yu + shu + ying
yubi = yu / zong
print('%s的总成绩是%d' % (name, zong))
print('语文成绩所占百分比是%.2f%%' %(yubi * 100))
Published 10 original articles · won praise 0 · Views 237

Guess you like

Origin blog.csdn.net/mango_kid/article/details/104774871