简体   繁体   English

我不知道如何用我的 Python 语句只显示一次错误消息

[英]I don't know how to only show error message once with my Python statement

I'm fairly new to Python/Flask and I am trying to get a code block to only show an error message in the UI once - (and to stop it from repeating depending on how many errors are found).我对 Python/Flask 还很陌生,我试图让代码块只在 UI 中显示一条错误消息 - (并根据发现的错误数量来阻止它重复)。 I've seen other questions answered about this issue, most responses using some form of break after a print statement...but from what I've tried, that isn't working for my code because I'm not just printing the error - the error message is being put into a variable that will then be shown to the user on the screen.我已经看到有关此问题的其他问题的回答,大多数响应在打印语句后使用某种形式的中断...但是从我的尝试来看,这不适用于我的代码,因为我不只是打印错误- 错误消息被放入一个变量中,然后将在屏幕上显示给用户。

error_messages = []

designer_returned_an_error = isinstance(result, str)
if designer_returned_an_error:
    print("Designer returned an error, not running additional checks")
    return {"message": result}, 400

# checks for screens with length of 1 or 0 in the list of lists
list1 = result[0]
print(list1)

for screens in list1:
    print(len(screens))
    if len(screens) <= 1:
        error_message = 'Based on these parameters you do not have enough items per screen to create a design.'
        error_messages.append(error_message)

当前用户界面消息

Maybe use a text file that is written to when an error is thrown, and in your if statement check if the written text is in the text file, so it only says there is an error when there isn't anything in the file.也许使用一个在抛出错误时写入的文本文件,并在你的 if 语句中检查写入的文本是否在文本文件中,所以它只会在文件中没有任何内容时说存在错误。 Like so:像这样:

f = open('examplename.txt', 'r+')
fileInfo = f.read()
error_messages = []

designer_returned_an_error = isinstance(result, str)
if designer_returned_an_error and fileInfo=="":
    print("Designer returned an error, not running additional checks")
    return {"message": result}, 400
    f.write("error already thrown")

# checks for screens with length of 1 or 0 in the list of lists
list1 = result[0]
print(list1)

for screens in list1:
    print(len(screens))
    if len(screens) <= 1:
        error_message = 'Based on these parameters you do not have enough items per screen to create a design.'
        error_messages.append(error_message)

You can break the loop if there is an error because is a blocked error, for example just add a break inside the error condition, also you can define your errors by code and set the message outside the loop:如果出现错误,您可以中断循环,因为这是一个阻塞错误,例如只需在错误条件内添加一个break ,您也可以通过代码定义错误并将消息设置在循环外:

error_messages = []

designer_returned_an_error = isinstance(result, str)
if designer_returned_an_error:
    print("Designer returned an error, not running additional checks")
    return {"message": result}, 400

# checks for screens with length of 1 or 0 in the list of lists
list1 = result[0]
print(list1)

for screens in list1:
    print(len(screens))
    if len(screens) <= 1:
        error_message = 'Based on these parameters you do not have enough items per screen to create a design.'
        error_messages.append(error_message)
        break

Or you can do the following:或者您可以执行以下操作:

error_messages = []
items_with_errors = {'screen':[]}

designer_returned_an_error = isinstance(result, str)
if designer_returned_an_error:
    print("Designer returned an error, not running additional checks")
    return {"message": result}, 400

# checks for screens with length of 1 or 0 in the list of lists
list1 = result[0]
print(list1)

for screens in list1:
    print(len(screens))
    if len(screens) <= 1:
        items_with_errors['screen'].append(screens)

if len(items_with_errors['screen']) > 0:
    error_messages.append(f'Based on these parameters you do not have enough items per screen to create a design. You must have at least {len(items_with_errors['screen'])} item(s) per screen')


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:我的代码出错,我不知道如何修复它 - Python: Error in my code and i don't know how to fix it Python:我的 if 语句不起作用但我没有收到任何错误? - Python: My if statement is not working but I don't get any error? 我不知道为什么我的“if”语句不起作用 - I don't know why my 'if' statement isn't working 当我不知道要花多长时间时,如何显示QProgressDialog? - How do I get my QProgressDialog to show up when I don't know how long it will take? 我的函数中的打印语句出现语法错误。 我不知道我做错了什么 - I get syntax error on my print statement in my function. I don't know what I am doing wrong 我不明白这个错误信息 (Python) - I don't understand this error message (Python) 由于某种原因,在我的代码中出现“索引超出范围”的错误,我实际上刚刚开始使用 Python,因此不知道如何解决这个问题 - In My Code There Is A Error Saying "Index Out Of Range" For Some Reason, I Actually Just Started Python And Hence Don't Know How To Solve This 我不知道如何在我的游戏中显示可供用户选择的课程列表? - I don't know how to show a list of classes for user to choose from in my game? 如果我不知道列表中有多少个值,则使用 SQL 语句 - SQL statement if I don't know how many values in list "<i>jinja if statement is not working.<\/i> jinja if 语句不起作用。<\/b> <i>I don&#39;t know how to fix it<\/i>我不知道如何解决它<\/b>" - jinja if statement is not working. I don't know how to fix it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM