print("first three:") for number in list1[0:3]: print(number) length=len(list1) location=length//2+1 print("middle three:") for number in list1[location-2:location+1]: print(number) print("last three:") for number in list1[-3:]: print(number)
5-10 检查用户名
1 2 3 4 5 6 7 8 9 10 11 12 13
#5-10 检查用户名 current_users = ['neruko','Chiyuru','chiyuruuu','ouuan','pieris05'] new_users = ['Neruko','aliceZ','chiyuruuu','chiyuruu','TA'] #取名鬼才真的编不出来名字,谢谢社友们 current_users2=[user.lower() for user in current_users]
for users in new_users: if users.lower() in current_users2: print(f"The name {users} has been used!") else: print(f"You can use the name {users}")
for pet in pets: pettype = pet['pet_type'] ownername = pet['owner'] print(f"The {pettype}'s owner is {ownername}") print("These are all the pets.")
6-9 喜欢的地方
1 2 3 4 5 6 7 8 9 10
#6-9 喜欢的地方 favorite_places = {'chiyuru':['Shanghai','London','Tokyo'], 'Neruko':['Beijing'], 'ouuan':['Beijing']} #谢谢社友,其实只是写了北京( for people in favorite_places.keys(): for city in favorite_places[people]: print(f"{people} loves {city}.")
print("Programme ended.")
6-11 城市
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#6-11 城市 cities = {'Shanghai':{ 'country':'China','pop':"2kw",'fact':'Best city in China'}, 'London':{ 'country':'Britain','pop':'1kw','fact':'Capital of Britain'}, 'Tokyo':{ 'country':'Japan','pop':'2kw','fact':'Heaven of ACGN'}} #这都是什么暴论
for city in cities.keys(): info = cities[city] place = info['country'] people = info['pop'] description = info['fact'] print(f"{city} is in {place}. There're {people} people live here. Chiyuru describe it as {description}. ")
print("These are the cities.")
7-4 披萨配料
1 2 3 4 5 6 7 8 9 10 11
#7-4 披萨配料 toppings = ['pepper','chicken','ketchup'] for topping in toppings: get = input(f"Please enter the addictive:{topping}\n") if get == 'quit': print("Thank you!") break elif get in toppings: print(f"You've added {get}.") else: print("You've got the wrong recipe!")
7-5 电影票
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#7-5 电影票 count = 1 while count <= 1: age = input("Please tell me your age:\n") ifint(age) < 3: ticket = 0 print(f"Your ticket is {ticket} dollar.") elifint(age) < 12: ticket = 10 print(f"Your ticket is {ticket} dollars.") elifint(age) > 12: ticket = 15 print(f"Your ticket is {ticket} dollars.") count = count + 1
while sandwich_orders: order = sandwich_orders.pop() finished_sandwiches.append(order) print(f"I made your {order} sandwich.") print("The finished sandwiches are:") for order in finished_sandwiches: print(f"{order} sandwich")
print("The pastrami has been sold out.") while'pastrami'in sandwich_orders: sandwich_orders.remove('pastrami')
while sandwich_orders: order = sandwich_orders.pop() finished_sandwiches.append(order) print(f"I made your {order} sandwich.")
8-5 大号T恤
1 2 3 4 5 6
#8-5 大号T恤 defmake_shirt(size='L',word='I love Python'): print(f"The T-shirt is {size} size with the sentence '{word}'.") make_shirt() make_shirt('M',) make_shirt(word='I love C')
flag = True while flag: singer = input("Please enter the name of the singer:") if singer=='q': flag = False break album = input("Please enter the name of the album:") if album == 'q': flag = False break print(make_album(singer,album)) print("The programme ended.")
8-11 发送消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#8-11 发送消息 msg = ['Hello!','Nice day!','Goodbye!'] defshow_messages(messages): for message in messages: print(message) show_messages(msg)
sent_msg = [] defsend_messages(messages): msg2 = messages[:] while msg2: message = msg2.pop() print(f"Message '{message}' is sent.") sent_msg.append(message) send_messages(msg) show_messages(msg) show_messages(sent_msg)
#9-5 用户 classUser: def__init__(self,firstName,lastName,notes,signature,login_attempts): self.firstName=firstName self.lastName=lastName self.notes=notes self.signature=signature self.login_attempts=login_attempts defdescribe_user(self): print(f"The user's name is {self.firstName.title()}{self.lastName.title()}") print(f"The user list the notes:{self.notes}.") print(f"The user list the signature:{self.signature}.") defgreet_user(self): print(f"Hello, {self.firstName.title()}{self.lastName.title()}!") defincrement_attempts(self): self.login_attempts = self.login_attempts + 1 print(f"Your login attempts are {self.login_attempts}") defreset_login_attempts(self): self.login_attempts=0 print(f"Your login attempts is {self.login_attempts}")
#9-7 管理员 classUser: def__init__(self,firstName,lastName,notes,signature,login_attempts): self.firstName=firstName self.lastName=lastName self.notes=notes self.signature=signature self.login_attempts=login_attempts defdescribe_user(self): print(f"The user's name is {self.firstName.title()}{self.lastName.title()}") print(f"The user list the notes:{self.notes}.") print(f"The user list the signature:{self.signature}.") defgreet_user(self): print(f"Hello, {self.firstName.title()}{self.lastName.title()}!") defincrement_attempts(self): self.login_attempts = self.login_attempts + 1 print(f"Your login attempts are {self.login_attempts}") defreset_login_attempts(self): self.login_attempts=0 print(f"Your login attempts is {self.login_attempts}") classAdmin(User): def__init__(self,firstName,lastName,notes,signature,login_attempts,privileges = ['can add post','can delete post','can ban user']): super().__init__(firstName, lastName, notes, signature, login_attempts) self.privileges = privileges defshow_privileges(self): for privilege in self.privileges: print(f"The user is an Admin.He/She {privilege}.")
#9-8 权限(换一种方式写9-7) classPrivileges: def__init__(self,privileges = ['can add post','can delete post','can ban user']): self.privileges = privileges defshow_privileges(self): for privilege in self.privileges: print(f"The user {privilege}.")
classUser: def__init__(self,firstName,lastName,notes,signature,login_attempts): self.firstName=firstName self.lastName=lastName self.notes=notes self.signature=signature self.login_attempts=login_attempts defdescribe_user(self): print(f"The user's name is {self.firstName.title()}{self.lastName.title()}") print(f"The user list the notes:{self.notes}.") print(f"The user list the signature:{self.signature}.") defgreet_user(self): print(f"Hello, {self.firstName.title()}{self.lastName.title()}!") defincrement_attempts(self): self.login_attempts = self.login_attempts + 1 print(f"Your login attempts are {self.login_attempts}") defreset_login_attempts(self): self.login_attempts=0 print(f"Your login attempts is {self.login_attempts}") classAdmin(User): def__init__(self,firstName,lastName,notes,signature,login_attempts): super().__init__(firstName, lastName, notes, signature, login_attempts) self.privileges = Privileges() Chiyuru = Admin('Feiyan','Ma','QWQ','QWQWQ',114514) Chiyuru.privileges.show_privileges()
9-14 彩票
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#9-14 彩票 from random import choice ticket = ('1','2','3','4','5','6','7','8','9','0','A','B','C','D','E')
my_ticket = ['1','4','A','8'] time = 0 flag = False while1-flag: time = time + 1 letter1 = choice(ticket) letter2 = choice(ticket) letter3 = choice(ticket) letter4 = choice(ticket) letters = [letter1,letter2,letter3,letter4] flag = (my_ticket == letters) print(letters) print(f"We have guessed {time} times.") print("The answer is correct!")
10-1 Python学习笔记
1 2 3 4 5 6 7 8 9 10 11 12 13
#10-1 Python学习笔记 withopen('pi.txt') as python_notes: content = python_notes.read() print(content) withopen('pi.txt') as python_notes: content_lines = python_notes.readlines()
notes = '' for line in content_lines: print(line.strip()) notes = notes + line.strip() print(notes)
10-2 C语言学习笔记
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#10-2 C语言学习笔记 withopen('pi.txt') as python_notes: content = python_notes.read() print(content.replace('Python','C')) withopen('pi.txt') as python_notes: content_lines = python_notes.readlines()
notes = '' for line in content_lines: print(line.strip().replace('Python','C')) notes = notes + line.strip() print(notes.replace('Python','C'))
10-4 访客名单
1 2 3 4 5 6 7 8
#10-4 访客名单 guest = [] for i inrange(0,5): guest.append(input("Please enter the name of your guest:")) withopen('guest.txt','a') as guest_write: for i inrange(0,5): guest_write.write(f'{guest[i]}\n')
10-5 调查
1 2 3 4 5 6 7
#10-5 调查 withopen('reasons.txt','a') as reasons: whileTrue: reason = input("Enter a reason why you love programming:") if reason =='quit': break reasons.write(f"{reason}\n")
10-7 加法计算器
1 2 3 4 5 6 7 8 9 10 11
#10-7 加法计算器 whileTrue: try: a = int(input("The first number:")) b = int(input("The second number:")) a+b except ValueError: print("You should try to enter a number") else: print(f"The answer is {a+b}.") break
for line in content: count1 = count1 + line.lower().count('the') count2 = count2 + line.lower().count(' the ') count3 = count3 + line.lower().count('the ') print(f"The results are {count1}, {count2} and {count3}.")
10-12 记住喜欢的数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#10-12 记住喜欢的数 import json
filename = 'lucky_number.json'
try: withopen(filename) as l_n: lucky_number = json.load(l_n) except FileNotFoundError: print("You haven't told us your lucky number!") number = input("Please enter your lucky number:") withopen(filename,'a') as l_n: json.dump(number,l_n) print("You have saved your lucky number!") else: print(f"Your lucky number is {lucky_number}.")
username = input("What is your name? ") filename = 'username.json' withopen(filename, 'w') as f: json.dump(username, f) return username
defgreet_user():
username = get_stored_username() if username: judge = input(f"Is {username} your username?Please enter yes/no.") if judge == 'yes': print(f"Nice to see you,{username}!") elif judge == 'no': username = get_new_username() print(f"We'll remember you when you come back, {username}!") else: username = get_new_username() print(f"We'll remember you when you come back, {username}!")
#不确定度。用的时候填充 data,修改其中的实验次数 “6”,根据情况修改已定系差和 B 类不确定度即可。 data =c(1,2,3,4,5,6) d = mean(data)-0.03#均值减去已定系差 sd(d)#标准偏差 tinv = qt(0.975,6-1)#前者常数,后者自由度 U_A = tinv*sd(d)/sqrt(6) U_B =0.004#B类不确定度,一般就是0 sqrt(U_A^2+U_B^2)#不确定度,一般就是U_A
#线性回归。用的时候修改 y 和 x 的值即可。 x =c(1,2,3,4,5,6) y =c(3,4,5,6,2,9) ans_1 = lm(y~x)#斜率不为零的拟合值 summary(ans_1)#显示具体值。其中Std.Error代表斜率截距A类不确定度 ans_2 = lm(y~x+0)#斜率为零的拟合值 summary(ans_2)
from plotly import offline from plotly.graph_objs import Bar,Layout import random
classDice: def__init__(self, faces): self.faces = faces defroll_dice(self): result = random.randint(1,self.faces) return result dice1 = Dice(6) dice2 = Dice(6) results1 = [dice1.roll_dice() for i inrange(1,1001)] results2 = [dice2.roll_dice() for i inrange(1,1001)] results = [results1[i]*results2[i] for i inrange(0,1000)]
frequencies = []
for value inrange(1,37): frequency = results.count(value) frequencies.append(frequency) print(frequencies)
x_values = list(range(1,37)) data = [Bar(x=x_values,y=frequencies)] x_axis_config = {'title':'result','dtick':1} y_axis_config = {'title':'frequency of the result'} my_layout = Layout(xaxis = x_axis_config,yaxis = y_axis_config) offline.plot({'data':data,'layout':my_layout},filename = '2Dice6.html')
conda create -n ai python=3.8 conda activate ai pip install -r requirements.txt
如果你是 NVIDIA 显卡的受害者,那么恭喜你可以使用 CUDA
加速的相关库。你可以理解成充分利用你的显卡的算力来做并行计算。如果你有一张显存大于
4 GB 的显卡就非常符合本次任务的要求:请删除
requirements.txt 中 torch==1.12.0
这一行,然后安装带有 CUDA 加速版本的 torch:
1 2 3 4
# Windows / Linux pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113 # macOS is not supported yet for CUDA :( # Link copied from https://pytorch.org/
准备数据集(5 p.t.s)
请从上面清华云盘的链接中下载数据集,然后解压到 data
目录下,保证 data 目录下直接存在
train、val、test 文件夹与
LICENSE 文件。