Python study notes - (2) loop

·Introduce the ciphertext input 
to the getpass library and
use
getpass.getpass("password:")
tips: This function is not easy to use in PyCharm, and can only be used on the command line

·Python forced indentation, omitting the prompts for code segments such as braces
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Vergil Zhang

import getpass #cipher   text input

print("hello python")

name = "Vergil Zhang"

print("My name is",name)
username = input("name:")
print(type(username))
age = int(input("age:"))
print(type(age))
job = input("job:")

#Three printing methods for user interaction 
message = """
username:%s
age:%d
job:%s
""" % (username, age, job)
print(message)

info = """
Name:{_name}
Age:{_age}
Job:{_job}
""" .format(_name = username,
            _age = age,
            _job = job)
print(info)

info2 = """
NAME:{0}
AGE:{1}
JOB:{2}
""" .format(username,age,job)
print(info2)

#Ciphertext input, this function is not easy to use in PyCharm, you can only use 
''' on the command line
user = input("userName:")
password = getpass.getpass("password:")

print(user,password)
'''

 

·Loop 
while:
else:

for i in range(10):
else:

for i in range(a,b,c): start from a and end at b, the step size is c, that is, a, a+c, a+2c, a+3c,...,b
#while,if,elif,else组合

age_of_oldboy = 56
count = 0

while count < 3 :
    guess_age = int(input("guess age:"))
    if guess_age == age_of_oldboy :
        print("yes, you got it.")
        break
    elif guess_age > age_of_oldboy :
        print("think smaller。。。")
    else :
        print("think bigger。。。")
    count += 1
#if count == 3:
#    print("no chance,fuck off")
else:
    print("no chance,fuck off")

# for loop optimized version 
for i in range(3 ):
    guess_age = int(input("guess age:"))
    if guess_age == age_of_oldboy :
        print("yes, you got it.")
        break
    elif guess_age > age_of_oldboy :
        print("think smaller。。。")
    else :
        print("think bigger。。。")
    count += 1
else:
    print("no chance,fuck off")

#Infinite play 
while count < 3 :
    guess_age = int(input("guess age:"))
    if guess_age == age_of_oldboy :
        print("yes, you got it.")
        break
    elif guess_age > age_of_oldboy :
        print("think smaller。。。")
    else :
        print("think bigger。。。")
    count += 1
    if count ==3 :
        continue_confirm = input("do you want to keep guessing?")
        if continue_confirm != 'n' :
            count = 0
else:
    print("no chance,fuck off")

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324913484&siteId=291194637