- تاریخ انتشار : دوشنبه ۲۲ بهمن ۱۴۰۳ - ۲۲:۱۲
- کد خبر : 1280 چاپ خبر
Ethereum: if statement with boolean within loop: ‘NoneType’ error
Ethereum: If a statement with logical loopu – confusing mistake As a creator working with Ethereum -based intellectual contracts, you are not one of you have experienced a tedious mistake that has caused many scratching your head. This question is seemingly harmless: a non -etype error trying to use logical value in the loop. Problem:
Ethereum: If a statement with logical loopu – confusing mistake
As a creator working with Ethereum -based intellectual contracts, you are not one of you have experienced a tedious mistake that has caused many scratching your head. This question is seemingly harmless: a non -etype error trying to use logical value in the loop.
Problem:
When the logical variable is used inside the loop, the whole expression can evaluate the wrong (or no python) despite the values assigned to the variables. This leads to unexpected behavior and incorrect results.
Code example:
Let’s look at the sample code of the code that shows this problem:
`Python
Def test_loop ():
Bool_var = True
_ Range (10):
If bool_var:
Print (Loops Iteration:, _)
`
In this example, we define the logical variable Bol_var and use it in the loop. However, the expression if bool_var:
will evaluate the wrong (or not) when the bool_var is true, so the loop misses every iteration.
NEETYPE ERROR:
As you can imagine, this behavior does not exactly correspond to the expected loop result. The loop should be continued until the condition or the maximum number of iterations is achieved. However, using the logical expression in the loop will eventually evaluate all iterations to the wrong (or not), so the wrong mistake “is not”.
Solutions:
Fortunately, there are some solutions to this problem:
- Use
any ()
feature : Instead of checking all the condition with “if bool_var:, use
any (bool_var _ range (10))At least one iteration is successful.
`Python
Def test_loop ():
Bool_var = True
_ Range (10):
If there are (bool_var):
Print (Loops Iteration:, _)
`
- Use
all ()
function: Similar to the previous solution, you can use the All (Bol_var _ range (10))
, which will only appreciate the truth if all iterations succeed.
Python
Def test_loop ():
Bool_var = True
_ Range (10):
If bool_var:
Print (Loops Iteration:, _)
`
- Avoid logical values to use loops : If possible, it is usually better to use other control structures such as “if” or “ELIF” statements to make your code more read and maintained.
`Python
Def test_loop ():
_ Range (10):
If not bool_var:
Pay attention to the change here!
Print (Loops Iteration:, _)
`
When applying these solutions, you should be able to avoid the disrupted “NEMETYPE” error when using logical values in loops. If your code is still confronted with questions, provide more context or detailed information about further assistance.
لینک کوتاه
برچسب ها
- نظرات ارسال شده توسط شما، پس از تایید توسط مدیران سایت منتشر خواهد شد.
- نظراتی که حاوی تهمت یا افترا باشد منتشر نخواهد شد.
- نظراتی که به غیر از زبان فارسی یا غیر مرتبط با خبر باشد منتشر نخواهد شد.
ارسال نظر شما
مجموع نظرات : 0 در انتظار بررسی : 0 انتشار یافته : 0