1、判断用户输入的用户名、密码和设置的是否一致
import getpass
_username = "wangsong"
_password = "123456"
username = input("username:")
password = getpass.getpass("password")
if _username == username and _password == password:
print("Welcome user {name} login....".format(name=username))
else:
print("Invalid username or password")
2、使用while和if循环,猜年龄,最多允许猜3次,3次内猜对了直接跳出循环,输错三次则报错。

#!/usr/bin/env python3.4
count = 0
while True:
if count == 3:
break
age_of_wangsong = 25
guess_age = int(input("Please guess age: "))
if guess_age == age_of_wangsong:
print("You guess successfuly...")
elif guess_age < age_of_wangsong:
print("You guess smaller...")
else:
print("You guess bigger...")
count +=1

#!/usr/bin/env python3.4
count = 0
while count < 3:
age_of_wangsong = 25
guess_age = int(input("Please guess Age: "))
if guess_age == age_of_wangsong:
print("Wow you Get it...")
break
elif guess_age < age_of_wangsong:
print("You guess smaller...")
else:
print("You guess bigger...")
count +=1
else:
print("you have tried too many times..fuck off")
3、使用for循环猜年龄,最多允许猜3次,3次内猜对了直接跳出循环,输错三次则报错。

for i in range(3):
age_of_wangsong = 25
guess_age = int(input("guess age:"))
if age_of_wangsong == guess_age:
print("You guess it successfully")
break
elif age_of_wangsong > guess_age:
print("You guess smaller...")
else:
print("You guess bigger...")
4、使用for循环打印0-9;使用for循环打印十以内偶数。

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

for i in range(0,10,2):
print("loop",i)
5、猜年龄,连续输错3次就问问用户要不要继续玩,如果想玩就继续,不想玩就退出。

count = 0
while count < 3:
age_of_wangsong = 25
guess_age = int(input("guess_age: "))
if guess_age == age_of_wangsong:
print("You guess it successfuly...")
continue_comfirm = input("Would you want to continue[Y/n]: ")
if continue_comfirm != 'n':
count = 0
else:
break
if guess_age < age_of_wangsong:
print("You guess it smaller...")
count +=1
if count == 3:
continue_comfirm = input("Would you want to continue[Y/n]: ")
if continue_comfirm != 'n':
count = 0
if guess_age > age_of_wangsong:
print("You guess it bigger...")
count +=1
if count == 3:
continue_comfirm = input("Would you want to continue[Y/n]: ")
if continue_comfirm != 'n':
count = 0
来源:https://www.cnblogs.com/kissrose/p/9491849.html
