1. Guessing game
- There is a sequence: 8 , 4 , 2 , 1 , 23 , 344 , 12
- Loop out the values of an array
- Find the sum of all values in a sequence
- Guess the number game: input any data from the keyboard, and judge whether the number is included in the sequence
The implementation code is as follows:
list1 = [8, 4, 2, 1, 23, 344, 12]
sum = 0
for x in list1:
print('数字的值:', x)
sum += x
print('和:%d' % sum)
num = int(input('请输入你要查找的数字:'))
if list1.count(num) == 0:
print('数字不存在!')
else:
print('存在')
operation result:
2. Display the names of 5 special offers on the console
The implementation code is as follows:
list1 = ["Nike背包", "Adidas运动衫", "李宁运动鞋", "Kappa外套", "361腰包"]
print("本次特价活动商品有:")
for x in list1:
print(x)
operation result:
3. Settlement of shopping amount
Output 5 shopping amounts and the total amount in the form of a table
- Implementation steps
- Create a container (list, tuple or dictionary) of length 5 to store the shopping amount
- Circularly input 5 shopping amounts and add up the total amount
- Use the cycle to output 5 shopping amounts, and finally output the total amount
The implementation code is as follows:
list = {}
sum = 0
for i in range(5):
pay_money = float(input("请输入第"+str(i+1)+"笔购物金额:"))
list[i+1] = pay_money
sum = sum+pay_money
print("序号 金额(元)")
for k,v in list.items():
print(k," ",v)
print("总金额 ",sum)
operation result:
4. Circularly input the grades of 5 students, sort them in ascending order and output the results
Tip: sort() method: sort the array in ascending order
The implementation code is as follows:
arrScore =[]
i = 1
while i<= 5:
s = float(input('请录入第%d个人的成绩:'%i))
i += 1
arrScore.append(s)
arrScore.sort()
print('学员成绩按升序排列:',arrScore)
operation result:
5. Character output in reverse order
- Sort a random set of characters
- Output in ascending and reverse order
The implementation code is as follows:
from random import shuffle
data=list(range(10))
shuffle(data)
print('原字符序列:',data)
print('升序排序后:',sorted(data))
print('逆序输出为:',list(reversed(sorted(data))))
operation result:
6. Insert characters into an ordered sequence
- Improve on the previous exercise
- A set of ordered character sequences a , b , c , e , f , p , u , z , insert a new character into this character sequence, and the character sequence is required to remain in order after insertion
The implementation code is as follows:
import bisect
a = [1, 2, 4, 8, 12, 14, 19]
item = 13
position = bisect.bisect(a, item)
print('原字符序列是:',a)
print('待插入的字符是:',item)
print('插入字符的下标是:',position) # 如果放到有序序列中,应该存在的索引位置
# 使用列表的insert方法插入对应位置
a.insert(position, item)
print('插入后的字符序列是:',a)
operation result:
Seven, ask for the lowest price
- Find the lowest mobile phone prices in 4 stores
Implementation steps
-
- Define containers (lists, tuples, dictionaries) to store prices and use loop input
- Define the variable min to save the current lowest price
- Compare min with the rest of the elements in the array in turn
The implementation code is as follows:
print('请输入四家店的价格:')
min_Price =0
arrPrice = []
i = 1
while i<= 4:
s = int(input('请输入第%d家店的价格:'%i))
i += 1
arrPrice.append(s)
arrPrice.sort()
for min_Price in arrPrice:
if s < min_Price:
min_Price = s;
print('最低的价格是:',min_Price)
operation result: