mercredi 5 décembre 2018

Function return with different conditional statement

I have a simple function, which I do like to call and return some values. Inside that function there is a if, elif and else statement, purpose is when if condition is met, return some values, it is when if and elif are not fulfilled, run and display what ever is under else statement. I have used a widget alert to flag and state the problem.

The problem is:

1- When the function calls, it returns just what ever is under else. despite the if statement is fulfilled.

2- Remove all codelines under else, just run if and elif, return some value if the conditions are met, otherwise returns TypeError: 'NoneType' object is not iterable.

The code:

from PyQt5 import QtCore, QtWidgets, QtGui

def fun( x, y, z):
    X = x
    Y = y
    Z = z

    for i in range(0,Z):
        R = i * X/Y

        if R == 10:
            return R, i
        elif 10 < R <= 45:
            return R, i
        else:
            print('Error') 
            app = QtWidgets.QApplication([])
            error_dialog = QtWidgets.QErrorMessage()
            error_dialog.showMessage('Error!! change values')
            app.exec_() 
            return R, i

Using these values to fulfill conditions.

result, prod = fun(10, 60, 100)
result, prod = fun(105, 60, 100)
result, prod = fun(10, 600, 100)

I want to keep the statements and return values as it is desired. Thanks for your help

Aucun commentaire:

Enregistrer un commentaire