python - 任何人都可以在此代码中找到任何错误或错误吗?
问题描述
我已经在这个代码上工作了几个小时,我仍然可以解决它。在函数内部构建一个永久循环(无限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=[])
如果有人找到任何修复错误的方法,请告诉我。提前致谢。
解决方案
你必须至少在这里把表格编码
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))
推荐阅读
- javascript - 切换侧面板时调整列大小
- spring - nginx代理后面的spring boot swagger2 - 找不到swagger-resources
- apache-nifi - 请求的资源 ListenHTTP 处理器上不存在“Access-Control-Allow-Origin”标头
- c++ - 获取向量元素时出错
- python - 没有重复的 Pandas merge_asof
- javascript - 如何在反应中更新日期占位符选项 onMouseOver
- python - 我应该怎么做而不是在 CuPy 中使用 numpy.vectorize?
- azure-api-management - 有没有办法在 APIM 策略中调用外部 Lookup REST 服务?
- java - 在 mediastore 中获取歌曲的封面
- python - python运算符不需要条件语句吗?