Input and output of Python and flow control statements

Source: Internet
Author: User


1. Get user input via Raw_input () function (visible)

[email protected] scripts]# cat input.py

#!/usr/bin/env python

#-*-Coding:utf8-*-

Raw_input ("Please enter user name:")

[email protected] scripts]# python input.py

Please enter your user name: Alex

[Email protected] scripts]#


2. Copy the contents of user input to a variable

[email protected] scripts]# cat input.py

#!/usr/bin/env python

#-*-Coding:utf8-*-

Name=raw_input ("Please enter user name:")

Print Name

[email protected] scripts]# python input.py

Please enter your user name: Alex

Alex

[Email protected] scripts]#


3. User input is not visible (Getpass module)

[email protected] scripts]# cat input.py

#!/usr/bin/env python

#-*-Coding:utf8-*-

Import Getpass

Pwd=getpass.getpass (">>>")

Print "####################"

Print pwd

[email protected] scripts]# python input.py

>>>

####################

Alex

[email protected] scripts]# python input.py

>>>

####################

Python


4. Process Control

[email protected] scripts]# cat if01.py

#!/usr/bin/env python

#-*-Coding:utf8-*-

Name = Raw_input ("Please enter user name:") #---> Input alex, both sides are Alex, indicating that both sides are values

# Compare instead of the same object in memory

If name = = "Alex": #---> Note: When assigning a value with an equal sign (=), if it is to be compared with a double equal sign (= =) # Here is the value and value of the comparison, not the memory address for comparison.

Print "Login Successful"

Else

Print "Login Failed"

[Email protected] scripts]#


is and double equals sign (= =) Difference:

= = refers to the value of two variables equal to, does not require the same piece of memory in the address

is refers to two variables in memory must be the same piece of address, instead of saying that two values are equal.


[Email protected] scripts]# cat if01.py

#!/usr/bin/env python

#-*-Coding:utf8-*-

Name = Raw_input ("Please enter user name:")

If name = = "Alex":

Print "Login Successful"

Else

Print "Login Failed"

################################################

Name= raw_input ("Please enter user name:") #输入alex

Name =raw_input ("Please enter user name:") #输入alex

If name = = "Eric":

Print "Normal"

elif name = = "Tony":

Print "Super"

elif name = = "Alex":

print "Super God"

Else

Print "illegal"


Control Script Example ①:


[email protected] scripts]# cat login01.py

#!/usr/bin/env python

#-*-Coding:utf8-*-

Import Getpass

Name = Raw_input ("Please enter user name:")

PWD = Getpass.getpass ("Please enter password:")

If name = = "Eric" and pwd = = "123":

Print "Login successful!" Normal, 123 "

elif name = = "Tony" and pwd = = "123":

Print "Login successful! Super, 123"

elif name = = "Alex" and pwd = = "123":

Print "Login successful!" Super God, 123 "

Else

Print "Login failed!"


Overwrite the above script:

[email protected] scripts]# cat login.py

#!/usr/bin/env python

#-*-Coding:utf8-*-

Import Getpass

Name = Raw_input ("Please enter user name:")

PWD = Getpass.getpass ("Please enter password:")

If name = = "Alex" or "name =" Eric "or name = =" Tony "and pwd = =" 123 ":

Print "Login Successful!"

If name = = "Alex":

Print "Super God, 123"

If name = = "Eric":

Print "Normal, 123"

If name = = "Tony":

Print "Super, 123"

Else

Print "Illegal user"

Print "Login failed!"


To rewrite the script further:

[email protected] scripts]# cat login02.py

#!/usr/bin/env python

#-*-Coding:utf8-*-

Import Getpass

Name = Raw_input ("Please enter user name:")

PWD = Getpass.getpass ("Please enter password:")

if pwd = = "123":

If name = = "Eric":

Print "Eric Normal"

elif name = = "Tony":

Print "Tony Super"

elif name = = "Alex":

print "Alex Super God"

Else

Print "Illegal user"

Else

Print "Login failed!"


This article from "Flat Light is true" blog, please be sure to keep this source http://cryan.blog.51cto.com/10837891/1709386

Input and output of Python and flow control statements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.