The username and password used to log on to the applet and the difference between input and raw_input ., Inputraw_input

Source: Internet
Author: User
Tags rounds

The username and password used to log on to the applet and the difference between input and raw_input ., Inputraw_input

I. This program needs to be implemented:

1. Set a fixed user name and password

2. Enter the user name and password correctly and print the correct logon information.

3. Only three logons are run.

II. The python version used this time is:

Windows version:

C: \ Users \ daisy> python -- version
Python 2.7.13rc1

Linux version:

Iii. design ideas:

1. Define the username variable: USER_name = "admin1"

2. Define the PASSWORD variable: PASSWORD = "admin123"

3. Because three logon attempts are required, you can use a loop.

4. Program in windows:

Follow these steps to write programs and perform operations in pycharm using raw_input:

USER_name = "admin1" # define the user name
PASSWORD = "admin123" # define a PASSWORD
For I in range (3): # execute 3 times
Input_name = raw_input ("input your name:") # enter the user name
Input_pass = raw_input ("input your passwd:") # enter the password.
If input_name = USER_name and input_pass = PASSWORD: # if the user name and PASSWORD are correct, the following operation does not indicate that the logon is successful.
Print ("you are right .")
Print ("login is right .")
Break # logon success bounce cycle
Else:
Print ("your name is wrong.") # An error is returned when the logon username and password are incorrect.
Else:
Print ("input too success count.") # logs are not successfully output after three rounds of logon.

Execution result:

Run the following three wrong user names and passwords:

Input your name: admin
Input your passwd: admin
Your name is wrong.
Input your name: admin
Input your passwd: admin
Your name is wrong.
Input your name: admin
Input your passwd: passwd
Your name is wrong.
Input too limit count.

Run the correct username and password:

Input your name: admin
Input your passwd: admin
Your name is wrong.
Input your name: admin1
Input your passwd: admin123
You are right.
Login is right.

Write programs and operations in pycharm using input:

USER_name = "admin1" # define the user name
PASSWORD = "admin123" # define a PASSWORD
For I in range (3): # execute 3 times
Input_name = input ("input your name:") # enter the user name
Input_pass = input ("input your passwd:") # enter the password
If input_name = USER_name and input_pass = PASSWORD: # if the user name and PASSWORD are correct, the following operation does not indicate that the logon is successful.
Print ("you are right .")
Print ("login is right .")
Break # logon success bounce cycle
Else:
Print ("your name is wrong.") # An error is returned when the logon username and password are incorrect.
Else:
Print ("input too success count.") # logs are not successfully output after three rounds of logon.

Test Error:

Input your name: admin
Traceback (most recent call last ):
File "C:/Users/daisy/PycharmProjects/s12/day1/login. py", line 6, in <module>
Input_name = input ("input your name :")
File "<string>", line 1, in <module>
NameError: name 'admin' is not defined

Process finished with exit code 1

Difference between input and raw_input:

>>> Input_name = input ("input your name :")
Input your name: 23
>>> Type (input_name)
Input your name: 23
>>> Type (input_name)
  <Type 'str'># Represents a string

It can be seen that the input output is an integer by default, and the raw_input output is a string by default:

Input solution: (enclose the input value in quotation marks to indicate a string)

Input your name:"Admin"
Input your passwd:"Admin123"
Your name is wrong.
Input your name:"Admin1"
Input your passwd:"Admin123"
You are right.
Login is right.

Conclusion: we can see that the raw_input string is better used in version 2.x. How does raw_input input the numeric type?

The method is as follows:

>>> Input_name = int (raw_input ("input your name:") # Use int to indicate Integer type:
Input your name: 23
>>> Type (input_name)
<Type 'int'> # It is indeed an int type:

>>> Input_name = float (raw_input ("input your name:") # use floating point:
Input your name: 23
>>> Type (input_name)
<Type 'float'> # It is indeed a floating point type:
>>>

4. Program in Linux:

[Root @ localhost ~] # Python3 -- version
Python 3.5.2 # version:
[Root @ localhost ~] # Python3
Python 3.5.2 (default, Apr 9 2017, 00:21:30)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

>>> USER_name = "admin1"

PASSWORD = "admin123"
# Input_num = 1
>>>>>>... # Input_count = 0
... For I in range (1, 4 ):
... Input_name = input ("input your name :")
... Input_pass = input ("input your passwd :")
... If input_name = USER_name and input_pass = PASSWORD:
... Print ("you are right .")
... Print ("login is right .")
... Break
... Else:
... Print ("your name is wrong .")
... Else:
... Print ("input too limit count .")
...
Input your name: admin
Input your passwd: admin
Your name is wrong.
Input your name: admin1
Input your passwd: admin123
You are right.
Login is right.
>>>

Because the 3.x version is simplified, no error is reported when you use input, and you do not need to enclose the input value in quotation marks to indicate characters.

Test the following input types:

Enter a number first:

>>> Input_name = input ("input your name :")
Input your name: 23

>>> Type (input_name)
<Class 'str'> # It is also a string

Similarly, you can use int, float, and other numeric types in 3.x. Refer to the above section.

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.