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