首页 > 解决方案 > 任何人都可以在此代码中找到任何错误或错误吗?

问题描述

我已经在这个代码上工作了几个小时,我仍然可以解决它。在函数内部构建一个永久循环(无限while循环)并在循环内部完成以下操作

我希望它使用一个变量来收集输入,该输入应该是'q'的整数才能退出。它应该检查输入字符串是否是数字(整数),如果是...将输入整数添加到报告变量。如果变量是“A”,则将数字字符添加到由新行分隔的项目字符串中。如果报表类型是 q,如果报表类型是“A”,则打印出所有输入的整数项和总和。如果报表类型为“T”,则打印总和仅在打印报表(“A”或“T”)后跳出 while 循环以结束函数。如果不是数字并且如果不是“Q”,则打印一条“输入无效”的消息。

def adding_report(report=[]):
report = []
at = input("Choose a report type: 'A' or 'T' : ")
while at.lower() != 'a' and at.lower() != 't':
    print('what?')
    at = input("Choose a report type: 'A' or 'T' : ")
while True:
    re = input("print an integer or 'Q' : ")
    if re.isdigit() is True:
        report.append(re)
        report.append('\n')
    elif re.startswith('q') is True:
        if at.lower() == 'a' is True:
            break
            print(report)
            print(sum(report))
        elif at.lower() == 't' is True:
            print(sum(report))
            break
        else:
            pass
    elif re.isallnum() is False and re.lower().startswith('q') is False:
        print('invalid response.')
    else:
        pass
adding_report(report=[])

如果有人找到任何修复错误的方法,请告诉我。提前致谢。

标签: pythonjupyter-notebook

解决方案


你必须至少在这里把表格编码

def adding_report(report=[]): # <-- You have errors here, defined function has nothing in it
report = [] # <-- I suggest tabulating this

这是不正确的用法,它有效

if re.isdigit() is True: # Not recommended

每当您检查某事是否为真时,请不要这样做

这是简单 if 语句的更好方法:

if re.isdigit(): # Recommended, will excecute when it returns any value, except (False and None)

如果您希望它执行,请在代码后移动 break:

  break # <-- this stops loop! you won't print anything that is after it
  print(report)
  print(sum(report))

推荐阅读