Python input and output and process control statements

Source: Internet
Author: User

Python input and output and process control statements
1. Use the raw_input () function to obtain user input (visible) [root @ Python scripts] # cat input. py #! /Usr/bin/env python #-*-coding: utf8-*-raw_input ("Enter the User name:") [root @ Python scripts] # python input. py enter the User name: alex [root @ Python scripts] #2. Copy the user input to a variable [root @ Python scripts] # cat input. py #! /Usr/bin/env python #-*-coding: utf8-*-name = raw_input ("Enter the User name:") print name [root @ Python scripts] # python input. for py, enter the user name alexalex [root @ Python scripts] #3. Invisible user input (getpass module) [root @ Python scripts] # cat input. py #! /Usr/bin/env python #-*-coding: utf8-*-import getpasspwd = getpass. getpass (">>> ") print "###################" print pwd [root @ Python scripts] # python input. py >>>################### alex [root @ Python scripts] # python input. py >>>################### python 4. Process Control [root @ Python scripts] # cat if01.py #! /Usr/bin/env python #-*-coding: utf8-*-name = raw_input ("Enter the User name:") # ---> input alex. At this time, both sides are alex, it means that the values on both sides are compared with #, instead of the same object in memory if name = "alex": # ---> Note: when assigning values, use an equal sign (= ), for comparison, use the double equal sign (=) # Here is the value and value for comparison, not the memory address for comparison. Print "Login successful" else: print "Login Failed" [root @ Python scripts] # is and double equals sign (=: = It means that the values of the two variables are equal. The same address in the memory is the same address. It means that the two variables must be the same address in the memory, instead of saying that the two values are equal, you can [root @ Python scripts] # cat if01.py #! /Usr/bin/env python #-*-coding: utf8-*-name = raw_input ("Enter the User name:") if name = "alex ": print "Logon successful" else: print "Logon Failed "################################### ############ name = raw_input ("Enter the User name: ") # Enter alexname = raw_input (" Enter the User name: ") # Enter alexif name =" eric ": print" normal "elif name =" tony ": print "super" elif name = "alex": print "super god" else: print "illegal" control script example ①: [root @ Python scripts] # cat login01.py #! /Usr/bin/env python #-*-coding: utf8-*-import getpassname = raw_input ("Enter the User name:") pwd = getpass. getpass ("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! Supergod, 123 "else: print" Login Failed! "Rewrite the above script: [root @ Python scripts] # cat login. py #! /Usr/bin/env python #-*-coding: utf8-*-import getpassname = raw_input ("Enter the User name:") pwd = getpass. getpass ("enter the password:") if name = "alex" or name = "eric" or name = "tony" and pwd = "123 ": print "Logon successful! "If name =" alex ": print" supergod, 123 "if name =" eric ": print" regular, 123 "if name =" tony ": print "super, 123" else: print "illegal user" print "Login Failed! "Rewrite the script further: [root @ Python scripts] # cat login02.py #! /Usr/bin/env python #-*-coding: utf8-*-import getpassname = raw_input ("Enter the User name:") pwd = getpass. getpass ("enter the password:") if pwd = "123": if name = "eric": print "eric" elif name = "tony ": print "tony super" elif name = "alex": print "alex" else: print "illegal user" else: print "Login Failed!"

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.