python基础:条件判断语句细节

    xiaoxiao2023-11-04  143

    password_list =['*#*#','12345'] def account_login(): password = input('Password: ') password_correct = password == password_list[-1] password_reset = password == password_list[0] if password_correct: print('Login success!') elif password_reset: new_password = input('Enter a new password: ') print('Your password has changed successfully!') account_login() else: print('Wrong password or invalid input!') account_login() account_login() 实验结果: ================== RESTART: /Users/apple/Documents/test2.py ================== Password: 1 Wrong password or invalid input! Password: 567 Wrong password or invalid input! Password: *#*# Enter a new password: zhoujian Your password has changed successfully! Password: zhoujian Wrong password or invalid input! Password: 12345 Login success! for a in 'zhou jian': print(a)

    结果: ================== RESTART: /Users/apple/Documents/test2.py ================== z h o u

    j i a n 嵌套循环:

    #Nest Loop for i in range(1,10): print(i) for i in range(1,10): for j in range(1,10): print('{} X {} = {}'.format(i,i,i*j))

    实验结果: ================== RESTART: /Users/apple/Documents/test2.py ==================

    1 2 3 4 5 6 7 8 9 1 X 1 = 1 1 X 1 = 2 1 X 1 = 3 1 X 1 = 4 1 X 1 = 5 1 X 1 = 6 1 X 1 = 7 1 X 1 = 8 1 X 1 = 9 2 X 2 = 2 2 X 2 = 4 2 X 2 = 6 2 X 2 = 8 2 X 2 = 10 2 X 2 = 12 2 X 2 = 14 2 X 2 = 16 2 X 2 = 18 3 X 3 = 3 3 X 3 = 6 3 X 3 = 9 3 X 3 = 12 3 X 3 = 15 3 X 3 = 18 3 X 3 = 21 3 X 3 = 24 3 X 3 = 27 4 X 4 = 4 4 X 4 = 8 4 X 4 = 12 4 X 4 = 16 4 X 4 = 20 4 X 4 = 24 4 X 4 = 28 4 X 4 = 32 4 X 4 = 36 5 X 5 = 5 5 X 5 = 10 5 X 5 = 15 5 X 5 = 20 5 X 5 = 25 5 X 5 = 30 5 X 5 = 35 5 X 5 = 40 5 X 5 = 45 6 X 6 = 6 6 X 6 = 12 6 X 6 = 18 6 X 6 = 24 6 X 6 = 30 6 X 6 = 36 6 X 6 = 42 6 X 6 = 48 6 X 6 = 54 7 X 7 = 7 7 X 7 = 14 7 X 7 = 21 7 X 7 = 28 7 X 7 = 35 7 X 7 = 42 7 X 7 = 49 7 X 7 = 56 7 X 7 = 63 8 X 8 = 8 8 X 8 = 16 8 X 8 = 24 8 X 8 = 32 8 X 8 = 40 8 X 8 = 48 8 X 8 = 56 8 X 8 = 64 8 X 8 = 72 9 X 9 = 9 9 X 9 = 18 9 X 9 = 27 9 X 9 = 36 9 X 9 = 45 9 X 9 = 54 9 X 9 = 63 9 X 9 = 72 9 X 9 = 81

    while 语句我们要注意停止

    count =0 while True: print('Repeat this line! ') count = count +1 if count ==5: break

    实验结果: ================== RESTART: /Users/apple/Documents/test2.py

    ================== Repeat this line! Repeat this line! Repeat this line! Repeat this line! Repeat this line!

    嵌套语句摇塞子程序: 1)摇骰子,记录points list 2)传入总数,评价大小 3)主函数 #Nest Loop

    import random def roll_dice(numbers=3, points =None): print('<<<<< ROOL THE DICE >>>>>') if points is None: points = [] while numbers >0: point =random.randrange(1,7) points.append(point) numbers = numbers -1 return points def roll_result(total): isBig = 11<= total <=18 isSmall = 3<= total <=10 if isBig: return 'Big' elif isSmall: return 'Small' def start_game(): print('<<<<Game Start! >>>>>>') choices =['Big','Small'] your_choice = input('Big or Small:') if your_choice in choices: points = roll_dice() total = sum(points) youWin = your_choice == roll_result(total) if youWin: print('The points are',points,'You Win!') else: print('The points are',points,'You Lose!') else: print('Invalid Words') start_game() start_game() 实验结果: ================== RESTART: /Users/apple/Documents/test2.py ================== <<<<Game Start! >>>>>> Big or Small:Big <<<<< ROOL THE DICE >>>>> The points are [5, 6, 2] You Win!

    自己根据赔率写的一个赌博程序: 学到了;变量的运用方法: #Nest Loop

    import random def roll_dice(numbers=3, points =None): print('<<<<< ROOL THE DICE >>>>>') if points is None: points = [] while numbers >0: point =random.randrange(1,7) points.append(point) numbers = numbers -1 return points def roll_result(total): isBig = 11<= total <=18 isSmall = 3<= total <=10 if isBig: return 'Big' elif isSmall: return 'Small' def money_gain(result,bet,money): if result == 'youWIn': money = int(money) + int(bet) print('you gain'+ bet +','+'you have '+money +'now') else: money = int(money) - int(bet) if money == 0: print('you loss ' +str(bet) + ','+ 'you have ' + str(money) +' now' +' please chongqian') else: print('you loss '+ str(bet) +','+'you have '+str(money) +'now') return money def start_game(money): print('<<<<Game Start! >>>>>>') if money <=0: print('GUN GUN GUN!!!!') else: choices =['Big','Small'] your_choice = input('Big or Small:') bet =input('please bet money: ') if your_choice in choices: points = roll_dice() total = sum(points) youWin = your_choice == roll_result(total) if youWin: print('The points are',points,'You Win!') money = money_gain('youWin',bet,money) start_game(money) else: print('The points are',points,'You Lose!') money= money_gain('youLoss',bet,money) start_game(moneyBig) else: print('Invalid Words') start_game(money) import sys money = 1000 start_game(money)

    实验结果:

    <<<<Game Start! >>>>>> Big or Small:Big please bet money: 1000 <<<<< ROOL THE DICE >>>>> The points are [6, 4, 1] You Win! you loss 1000,you have 0 now please chongqian <<<<Game Start! >>>>>> GUN GUN GUN!!!!

    python的四种数据格式

    最新回复(0)