
Lv.1
一直摸鱼一直爽!嘿哈!
签名:编程使我快乐~~
在 星光明月雀谁有!!! 中回复

我有 星光烈火龙 & 星光星月猫
配上 AI巴格 & 雨师 & 狸忍 & 夜魅鼠 送你元气
外加 赤星三春牛 全家桶~~
2021-08-08T11:25:42 点赞:0
在 编程猫把传说源码蛋给我吐出来!!!!!!!!!!!!!!!!!!!!!!!!!!!! 中回复
赤星三春牛,还我: 8赤星蛋(喵,现在还没弄到赤星灰灰鼠)
2021-08-08T11:38:01 点赞:0
在 【叶子】新手问题帖 中回复
回答叶子(什么是函数):
你可以理解函数就是一个封装了代码的盒子,随时随地都可以调用
Python 中的函数:
代码:
def printThings(things):
print(things) # 封装的代码
printThings("BZIClaw")
输出:
BZIClaw
注意的是函数并不能直接修改外部的变量,比如
a = 10
b = 10
sum = 0 # 在函数外定义了一个 sum
def sum():
sum = a + b # 在函数中定义了一个 sum
return sum
print(sum()) # 20
print(sum) # 0
因此我们需要使用 global 来声明为可修改的全局变量
代码如下:
a = 10
b = 10
sum = 0 # 在函数外定义了一个 sum
def sum():
global sum # 使用函数外的 sum
sum = a + b # 对 sum 进行操作
return sum
print(sum()) # 20
print(sum) # 20
如果觉得有帮助,希望可以点个赞或关注~~
2021-08-10T15:46:06 点赞:2
在 【便签】‘回 BZIClaw’ 中回复
我已经检查了,代码有些小问题
首先是你没有用 file.close
其次是我选择 2 后自动退出了
感谢您对文档的关注~~
2021-08-10T15:50:56 点赞:0
在 【便签】‘回 BZIClaw’ 中回复
电子便签任务的代码如下:
import datetime
notes = []
note = {}
newNote = open("note.txt", "w")
newNote.close()
print("================= 便签本 =================")
while True:
choice = input("输入 1 写便签,输入 2 查询便签\n输入 3 浏览全部便签,输入 q 退出系统\n")
if choice.strip("") == "q":
print("以退出电子便签本软件系统~~")
break
elif choice.strip("") == "1":
order = datetime.datetime.now()
order = format(order, "%Y-%m-%d %H:%M:%S")
word = input("")
note[order] = word
file = open("note.txt", "a")
file.write(order + "&" + word + "\n")
file.close()
elif choice.strip("") == "2":
string = input("请输入要查询的文字:")
file = open("note.txt")
while True:
line = file.readline().strip('').strip("\n")
if line == "":
break
if string in line:
lines = line.split("&")
print(lines[0])
print(" " + lines[1])
file.close()
elif choice.strip("") == "3":
file = open("note.txt")
while True:
line = file.readline().strip('').strip("\n")
if line == "":
break
lines = line.split("&")
print(lines[0])
print(" " + lines[1])
file.close()
请查收~~
2021-08-10T16:09:59 点赞:0