python - Chapter 02 - Ciphertext, if else judgment and while, for loop

1. Write the username and password and then encrypt it.

import getpass
username = input("username")
password = getpass.getpass("password")
print(username,password)

 

Variable secrets require modules to call standard libraries (import libraries, which do not require installation, are called standard libraries)

import getpass, which turns plaintext into ciphertext

import getpass
username = input("username")
password = getpass.getpass("password")
print(username,password)

2. To judge whether the username and password are correct, we need to judge.

username_ = "han"
password_ =  "123"
username = input("username:")
password = input("password:")
print(username,password)

if username_ == username and password_ == password:
    print("welcome  login...")
else:
    print("Error in password or username.")

There are no separators in python, so there are corresponding coercive indentations, provincial concluding remarks. If there are always errors, there is no problem with the code written, so be sure to check the indentation problem.

3. Judging Age

age_ = 30
age = int(input("Enter your age:"))
if age == age_:
    print("you are right!")
elif age_ < age:
    print("It is high")
else :
    print("It is low!")

In python, if-eiif-lese has a simpler cyclic structure, remember to add ":" in the cyclic body.

4.while cycle

Judging age wants to add functionality so that it can guess three times

while True:
    if count == 3:
        break
    age_ =30
    age = int(input("Enter your age:"))
    if age == age_:
        print("you are right!")
        break
    elif age_ < age:
        print("It is high")
    else:
        print("It is low!")

    count += 

Optimize:

count = 0
while count < 3: age_ =30 age = int(input("Enter your age:")) if age == age_: print("you are right!") break elif age_ < age: print("It is high") else: print("It is low!") count += 1 if count == 3: #else: print("You have printed too many times, please re-login!")

5.for cycle

for i in range(10):
    print("loop",i)

Want to skip one print one

for i in range(0,10,2):
    print("loop",i)

 

Change the while loop to the for loop

for i in range(3):
    age_ =30
    age = int(input("Enter your age:"))
    if age == age_:
        print("you are right!")
        break
    elif age_ < age:
        print("It is high")
    else:
        print("It is low!")

The use of range () can be found in python's function essay

Tags: PHP Python

Posted on Thu, 10 Oct 2019 14:34:50 -0700 by nagasea