鱼C论坛

 找回密码
 立即注册
查看: 3197|回复: 7

[已解决]python3 一个重复执行程序的问题

[复制链接]
发表于 2018-10-10 21:18:34 | 显示全部楼层 |阅读模式
8鱼币
  1. import os.path as op

  2. def check_file_size():
  3.       
  4.       """
  5.             本函数用于查看一个文件的大小,以字节的格式
  6.       """
  7.       
  8.       check_file = input("请输入一个文件地址,查看它的大小: ")
  9.       while not op.isfile(check_file):
  10.             check_file = input("查看大小操作的文件地址是错误的,请重新输入: ")
  11.             if op.isfile(check_file):
  12.                   break
  13.       print(f"{op.getsize(check_file)} 字节")

  14.       # 是否继续执行程序
  15.       print("是否重新执行程序?1 :是,2 :否")
  16.       continue_program = int(input("1 or 2: "))
  17.       while continue_program == 1:
  18.             check_file_size()
  19.             if continue_program == 2:
  20.                   break
  21.       print("已关闭程序")
  22. check_file_size()
复制代码


屏幕快照 2018-10-10 下午9.14.20.png


我在终端执行完程序后,无法关闭程序,求助:这个bug是怎么回事
最佳答案
2018-10-10 21:18:35
类十三 发表于 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 的还没解决?

最佳答案

查看完整内容

代码的第一行与最后一行就是接口,一个进一个出。 你的死循环是因为递归调用 while continue_program == 1: check_file_size() # 假设说调用并成功退出函数,continue_program 还是 == 1,所以死循环了。可以加上 break 或者 return if continue_program == 2: break 我给你的是 if continue_program == 1: ... #如果 ... 执行完毕后,不会去 else,就结束了,有结束的接口 ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-10 21:18:35 | 显示全部楼层    本楼为最佳答案   
类十三 发表于 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 的还没解决?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-10-10 21:19:08 | 显示全部楼层
问题就出现在“是否继续执行程序处的代码”
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-10 21:33:40 | 显示全部楼层
  1. import os.path as op

  2. def check_file_size():
  3.       
  4.       """
  5.             本函数用于查看一个文件的大小,以字节的格式
  6.       """
  7.       
  8.       check_file = input("请输入一个文件地址,查看它的大小: ")
  9.       while not op.isfile(check_file):
  10.             check_file = input("查看大小操作的文件地址是错误的,请重新输入: ")
  11.             if op.isfile(check_file):
  12.                   break
  13.       print(f"{op.getsize(check_file)} 字节")

  14.       # 是否继续执行程序
  15.       print("是否重新执行程序?1 :是,2 :否")
  16.       continue_program = int(input("1 or 2: "))
  17.       while continue_program not in [1,2]:
  18.           print("是否重新执行程序?1 :是,2 :否")
  19.           continue_program = int(input("1 or 2: "))
  20.       if continue_program == 1:
  21.             check_file_size()
  22.       else:
  23.           return
  24.       print("已关闭程序")
  25.       
  26. check_file_size()
复制代码
结束的接口没有,死循环了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-10-11 17:58:22 | 显示全部楼层
claws0n 发表于 2018-10-10 21:33
结束的接口没有,死循环了

老哥能不能提点一下,没学习过接口,上网查了半天,没看懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-11 19:52:05 | 显示全部楼层
本帖最后由 waitforlove 于 2018-10-11 19:54 编辑
  1. import os.path as op

  2. def check_file_size():
  3.       print("是否重新执行程序?1 :是,2 :否")
  4.       continue_program = int(input("1 or 2: "))
  5.       
  6.       if continue_program == 2:
  7.             print("已关闭程序")
  8.       elif continue_program == 1:
  9.             while True:
  10.                   check_file = input("请输入一个文件地址,查看它的大小: ")
  11.                   while not op.isfile(check_file):
  12.                         check_file = input("查看大小操作的文件地址是错误的,请重新输入: ")
  13.                         if op.isfile(check_file):
  14.                               print(f"{op.getsize(check_file)} 字节")
  15.                               break
  16.            check_file_size()

  17. check_file_size()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-10-16 18:49:30 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-10-16 18:56:34 | 显示全部楼层
最后敲定的答案:

  1. import os.path as op

  2. def check_file_size():
  3.       """
  4.             本函数用于查看一个文件的大小,以字节的格式
  5.             continue_program 变量用于判断是否继续执行程序
  6.             op.isfile() 用于判断文件地址是否是一个正确的文件
  7.             op.getsize() 可以返回文件的大小
  8.       """

  9.       
  10.       continue_program = 1
  11.       
  12.       if continue_program == 2:
  13.             print("已关闭程序")
  14.       elif continue_program == 1:
  15.             while True:
  16.                   check_file = input("请输入一个文件地址,查看它的大小: ")
  17.                   while not op.isfile(check_file):
  18.                         check_file = input("查看大小操作的文件地址是错误的,请重新输入: ")
  19.                   if op.isfile(check_file):
  20.                         print(f"{op.getsize(check_file)} 字节")
  21.                         break
  22.                   
  23.             print("是否重新执行程序?1 :是,2 :否")
  24.             continue_program = int(input("1 or 2: "))
  25.             
  26.             if continue_program == 1:
  27.                   check_file_size()

  28. check_file_size()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 00:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表