简体   繁体   中英

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

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


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM