程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] Python exercises

編輯:Python

List of articles

  • Class practice 1
  • Class practice 2
  • Class practice 3
  • Class practice 4

explain : The exercises in this blog are from b standing The course

Class practice 1

Title Description
Comprehensive use if Related knowledge of statements , Realize the effect of stone scissors cloth game . Display the following message :

Please enter : scissors (0), stone (1), cloth (2):

The user enters numbers 0-2 A number in , Compared with the number randomly generated by the system, the result information is given .

Your input is : scissors (0)
Randomly generated numbers are :1
ha-ha , Did you lose :)

Tips : Consider as much as possible in case of abnormal input , Make the program work properly .
It is recommended to use time 15~20 minute .

Reference code

import random
b = random.randint(0, 2)
a = input(" Please enter : scissors (0), stone (1), cloth (2):")
if a in ['0', '1', '2']:
a = int(a)
print(" The randomly generated number is :%d" % b)
if (a, b) in [(0, 1), (1, 2), (2, 0)]:
print(" ha-ha , Did you lose :)")
elif a == b:
print(" It's even =_=")
else:
print(" You win !!!")
else:
print(" illegal input ")

Class practice 2

Title Description
Print the multiplication table , The display effect is as follows :

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

Reference code

for i in range(1, 10):
for j in range(1,i+1):
print("%d*%d=%d" % (i, j, i*j), end=" ")
print(end="\n")

Class practice 3

Title Description
The list of existing products is as follows :

  1. products = [[“iphone”, 6888], [“MacPro”, 14800], [“ millet 6”, 2499],[“Coffee”, 31], [“Book”, 60], [“Nike”, 699]], You need to print out the following format :

----- List of goods -----
0 iphone 6888
1 MacPro 14800
2 millet 6 2499
3 Coffee 31
4 Book 60
5 Nike 699

2. According to the above products List write a loop , Keep asking users what they want to buy , The user selects an item number , Add the corresponding product to the shopping cart , End user input q Exit time , Print a list of purchased items .

Reference code

products = [["iphone", 6888], ["MacPro", 14800], [" millet 6", 2499],
["Coffee", 31], ["Book", 60], ["Nike", 699]]
print("----- List of goods -----")
i = 0
for product in products:
print("%d %s %d" % (i, product[0], product[1]))
i += 1
sum = 0
cu_list = []
while True:
num = input(" Please enter your purchase item number :")
if num in ['0', '1', '2', '3', '4', '5']:
num = int(num)
sum += products[num][1]
cu_list.append(products[num][0])
elif num == 'q':
print(" Shopping list :", end="")
for i in cu_list:
print(i, end=" ")
print(" Amount is %d element " % sum)
break
else:
print(" illegal input ")

Class practice 4

Title Description

  1. Apply knowledge of file operations , adopt python Create a new file gushi.txt, Select an ancient poem to write to the file
  2. Another function , Read the specified file gushi.txt, Copy content to copy.txt in , And output on the console “ Copy complete ”
  3. Tips : Defining the 2 A function , Complete the operation of reading and writing files
    Perfect the code as much as possible , Add exception handling

Reference code

def writeGushi():
f = open("gushi.txt", "w")
# f.write(" Li shangyin ")
f.write(" Untitled \n It's hard to see each other , The east wind is weak, and flowers are broken .\n Spring silkworms to the end of silk , When the wax torch turns grey, tears begin to dry .\n Xiaojing but worry about Yunbin change , When you sing at night, you should feel the cold moonlight .\n There is no way to go to Pengshan , The birds are eager to visit .")
f = open("gushi.txt", "r")
while True:
content = f.readlines()
if len(content) == 0:
break
print(content)
f.close()
def readGushi():
try:
f1 = open("gushi.txt", "r")
f2 = open("copy.txt", "w")
while True:
content = f1.readline()
if len(content) == 0:
f1.close()
f2.close()
print(" Copy complete ")
break
else:
f2.write(content)
except Exception as result:
print(" There are abnormal :%s" % result)
writeGushi()
readGushi()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved