【问题标题】:Can anyone find any bugs or mistakes in this code?任何人都可以在此代码中找到任何错误或错误吗?
【发布时间】:2019-06-15 00:52:29
【问题描述】:

我已经研究这个代码几个小时了,我仍然可以解决它。 在函数内部构建一个永久循环(无限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=[])

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

【问题讨论】:

  • 缩进在 Python 中很重要。您发布的代码没有正确缩进 - 这是您在代码中使用它的方式吗?如果没有,您应该编辑您发布的示例以准确反映您的代码。

标签: python jupyter-notebook


【解决方案1】:

您必须至少在此处将表格写入代码

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))

【讨论】:

    【解决方案2】:

    我猜你可能正在尝试做这样的事情?我对你的代码做了一些修改,并在出现错误的地方做了一些 cmets。

    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' : ")
            print(re)
            if re.isdigit(): # Remove is true
                report.append(int(re)) # Add integer to list. Use int() to convert string to int
    
            elif re.startswith('q'): # Remove is true
                if at.lower() == 'a':
                    print(report) # This two prints MUST be above de break statement in ordered to be executed
                    print(sum(report))
                    break
                elif at.lower() == 't': # Remove is true
                    print(sum(report))
                    break
                else:
                    pass
    
            elif not re.isdigit() and not re.lower().startswith('q'): # Replaced is False with not
                print('invalid response.')
            else:
                pass
    adding_report(report=[])
    

    【讨论】:

    • 我仍然收到错误 unsupported operand type(s) for +: 'int' and 'str'。此外,我的代码应该打印由新行分隔的所有整数。很抱歉你在我的第一篇文章中没有看到这个。
    • 您在哪一行收到该错误?我已经测试了上面的代码并且工作正常。请记住,您必须使用 int() 函数将控制台输入的数字转换为整数。此外,为了打印由新行分隔的整数,您可以遍历报告列表并打印每个整数,例如:for number in report: print(number)
    • 谢谢。另外,我在第 16 行和第 18 行遇到了这个错误。
    猜你喜欢
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    相关资源
    最近更新 更多