日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python基础第一天

發布時間:2023/12/18 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python基础第一天 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、內容

?

?

?

?

?

?

二、練習

練習1

題目:使用while循環輸出1 2 3 4 5 6 8 9 10

?

方法一:

圖示:

代碼:

count = 1 while count < 11:if count != 7:print(count)count += 1

?輸出結果:

1 2 3 4 5 6 8 9 10 View Code

?

?

方法二:

圖示:

代碼:

count = 0 while count < 10:count += 1if count == 7:continueprint(count)

輸出結果:

1 2 3 4 5 6 8 9 10 View Code

?

?

?

練習2

題目:求1-100的所有數的和

圖示:

代碼:

sum = 0 count = 1 while count < 101:sum = sum + countcount += 1 print(sum)

輸出結果:

5050

?

?

?

練習3

題目:輸出 1-100 內的所有奇數

圖示:

代碼:

count = 1 while count < 101:if count % 2 != 0:print(count)count += 1

輸出結果:

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 View Code

?

?

?

?

練習4

題目:輸出 1-100 內的所有偶數

圖示:

代碼:

count = 1 while count < 101:if count % 2 == 0:print(count)count += 1

輸出結果:

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 View Code

?

?

?

練習5

題目:求1-2+3-4+5 ... 99的所有數的和

圖示:

代碼:

count = 1 sum1 = 0 sum2 = 0 while count < 100:if count % 2 != 0:sum1 = sum1 + countelse:sum2 = sum2 - countcount += 1 print(sum1 + sum2)

輸出結果:

50

?

?

?

?

練習6

題目:用戶登陸(三次機會重試)

?

步驟一:實現三次登陸的基本功能

圖示:

代碼:

user = 'knight' pwd = 'dk123' count = 1 while True:if count == 4:print('Too many times!')breakusername = input('Please enter your username:')password = input('Please enter your password:')if username == user and password == pwd:print('Login successfully!')breakelse:print('The username or password you entered is incorrect,Please try again!')count += 1

?

?

?

步驟二:去除BUG并添加功能

1、去除用戶輸入用戶名時左右兩邊的空格

2、判斷用戶名是否有值,如果沒有則反復讓用戶輸入,并提示請輸入用戶名

3、判斷密碼是否有值,如果沒有則反復讓用戶輸入,提示請輸入密碼

4、用戶每輸錯一次便告之用戶還有多少次機會

圖示:

代碼:

user = 'knight' pwd = 'dk123' count = 1 while True:if count == 4:print('Too many times!')breakusername = input('Please enter your username:').strip()password = input('Please enter your password:')if not username:print('The username you entered is blank, please re-enter')continueelif not password:print('The password you entered is blank, please re-enter')continueif username == user and password == pwd:print('Login successfully!')breakelse:print('The username or password you entered is incorrect,Please try again!')print('You still have %s chances'%(3-count))count += 1

  

?

?

步驟三:三次登陸鎖定

擴展:當同一個用戶名的輸錯次數超過三次時,鎖定該用戶名

圖示:

代碼:

user = 'knight' pwd = 'dk123' count = 1 times = [] # 用于統計密碼輸錯時的用戶名的個數with open('blacklist.txt', 'r', encoding='utf-8') as f_black:black_line = f_black.readlines() # 將黑名單內容以列表的形式讀出來# print(black_line)while True:# 當登陸次數超過3次時提示登陸次數過多,退出程序if count == 4:print('Too many times!')breakusername = input('Please enter your username:').strip()password = input('Please enter your password:').strip()# 判斷用戶名是否為空if not username:print('The username you entered is blank, please re-enter')continue# 判斷密碼是否為空elif not password:print('The password you entered is blank, please re-enter')continue# 判斷用戶名再加上"\n"是否在黑名單的列表里,如果在則提示用戶已鎖定再退出程序if username + '\n' in black_line:print('Sorry, your account has been locked!')break# 判斷用戶名和密碼是否匹配if username == user and password == pwd:print('Login successfully!')breakelse:print('The username or password you entered is incorrect,Please try again!')print('You still have %s chances' % (3 - count)) # 如果不匹配則提示用戶名或密碼錯誤并告之用戶還有幾次機會times.append(username) # 將錯誤的用戶名加入至事先定義的times列表中,if times.count(username) == 3: # 然后計算加入的用戶名的個數是否等于三個,如果是則寫入至黑名單文件中。with open('blacklist.txt', 'a', encoding='utf-8')as f:f.write(username+'\n')count += 1

?

?

?

練習7

題目:猜年齡

步驟一:實現基本邏輯

圖示:

代碼:

age = 28 while True:user = int(input('Please enter a number:')) # 強轉為整型if user > age:print('Try smaller!')elif user < age:print('Try bigger!')else:print('You got it!')break

?

?

?

?

步驟二:去除BUG并添加功能

1、去除BUG,當用戶輸入其它字符時讓其重新輸入

2、添加猜錯時給予三次機會功能

圖示:

代碼:

age = 28 count = 0 while True:if count == 3:print('You guess too many times')breakuser = input('Please enter a number:').strip()if not user.isdigit():print('Please try again,and you must enter a number!')continueelse:user = int(user)if user > age:print('Try smaller!')elif user < age:print('Try bigger!')else:print('You got it!')breakcount += 1

?

?

?

?

?

三、英語單詞

必? 須? 背? 誦? 出? 來? !

1、Programming

['proɡr?m??]? ? n.?編程

?

2、Programming??language

['proɡr?m??]???['l??ɡw?d?]? ?n.編程語言

?

3、high-level language

?

[,ha?'l?vl]??['l??ɡw?d?]? ?n.高級語言

?

4、machine?language

[m?'?in]??['l??ɡw?d?]? ?n.機器語言

?

5、assembly language

?[?'s?mbli]?['l??ɡw?d?]? ?n.匯編語言

?

6、variable

?['v?r??bl]? ?n.?變量?

?

7、memory

['m?m?ri]? ?n.內存

?

8、binary

['ba?n?ri]? ?n.二進制

?

9、decimalism

['desim?liz?m]? ? n.十進制

?

10、octal?

['ɑktl]? ? adj.八進制的

?

11、hexadecimal

[,h?ks?'d?s?ml]? ? n.十六進制

?

12、file

?[fa?l]? ? n.文件

?

13、directory

[d??r?kt?ri; (also) da??r?kt?ri]? ?n.目錄

?

14、input

?['?n'p?t]? ? vt.輸入

?

轉載于:https://www.cnblogs.com/fyknight/p/8007463.html

總結

以上是生活随笔為你收集整理的Python基础第一天的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。