explain : The exercises in this blog are from b standing The course
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 ")
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")
Title Description
The list of existing products is as follows :
----- 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 ")
Title Description
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()