python3 一个重复执行程序的问题
import os.path as opdef check_file_size():
"""
本函数用于查看一个文件的大小,以字节的格式
"""
check_file = input("请输入一个文件地址,查看它的大小: ")
while not op.isfile(check_file):
check_file = input("查看大小操作的文件地址是错误的,请重新输入: ")
if op.isfile(check_file):
break
print(f"{op.getsize(check_file)} 字节")
# 是否继续执行程序
print("是否重新执行程序?1 :是,2 :否")
continue_program = int(input("1 or 2: "))
while continue_program == 1:
check_file_size()
if continue_program == 2:
break
print("已关闭程序")
check_file_size()
我在终端执行完程序后,无法关闭程序,求助:这个bug是怎么回事 类十三 发表于 2018-10-11 17:58
老哥能不能提点一下,没学习过接口,上网查了半天,没看懂,
代码的第一行与最后一行就是接口,一个进一个出。
你的死循环是因为递归调用
while continue_program == 1:
check_file_size() # 假设说调用并成功退出函数,continue_program 还是 == 1,所以死循环了。可以加上 break 或者 return
if continue_program == 2:
break
我给你的是 if continue_program == 1: ... #如果 ... 执行完毕后,不会去 else,就结束了,有结束的接口
就算是你多次选择 1,执行完毕之后,都是结束语句,所以递归调用可以一层一层返回
nonlocal 的还没解决? 问题就出现在“是否继续执行程序处的代码” import os.path as op
def check_file_size():
"""
本函数用于查看一个文件的大小,以字节的格式
"""
check_file = input("请输入一个文件地址,查看它的大小: ")
while not op.isfile(check_file):
check_file = input("查看大小操作的文件地址是错误的,请重新输入: ")
if op.isfile(check_file):
break
print(f"{op.getsize(check_file)} 字节")
# 是否继续执行程序
print("是否重新执行程序?1 :是,2 :否")
continue_program = int(input("1 or 2: "))
while continue_program not in :
print("是否重新执行程序?1 :是,2 :否")
continue_program = int(input("1 or 2: "))
if continue_program == 1:
check_file_size()
else:
return
print("已关闭程序")
check_file_size()结束的接口没有,死循环了 claws0n 发表于 2018-10-10 21:33
结束的接口没有,死循环了
老哥能不能提点一下,没学习过接口,上网查了半天,没看懂{:5_104:},{:10_266:} 本帖最后由 waitforlove 于 2018-10-11 19:54 编辑
import os.path as op
def check_file_size():
print("是否重新执行程序?1 :是,2 :否")
continue_program = int(input("1 or 2: "))
if continue_program == 2:
print("已关闭程序")
elif continue_program == 1:
while True:
check_file = input("请输入一个文件地址,查看它的大小: ")
while not op.isfile(check_file):
check_file = input("查看大小操作的文件地址是错误的,请重新输入: ")
if op.isfile(check_file):
print(f"{op.getsize(check_file)} 字节")
break
check_file_size()
check_file_size() waitforlove 发表于 2018-10-11 19:52
谢谢老哥的方案 最后敲定的答案:
import os.path as op
def check_file_size():
"""
本函数用于查看一个文件的大小,以字节的格式
continue_program 变量用于判断是否继续执行程序
op.isfile() 用于判断文件地址是否是一个正确的文件
op.getsize() 可以返回文件的大小
"""
continue_program = 1
if continue_program == 2:
print("已关闭程序")
elif continue_program == 1:
while True:
check_file = input("请输入一个文件地址,查看它的大小: ")
while not op.isfile(check_file):
check_file = input("查看大小操作的文件地址是错误的,请重新输入: ")
if op.isfile(check_file):
print(f"{op.getsize(check_file)} 字节")
break
print("是否重新执行程序?1 :是,2 :否")
continue_program = int(input("1 or 2: "))
if continue_program == 1:
check_file_size()
check_file_size()
页:
[1]