Why is this python while loop not ending?

2024/7/4 15:39:44

I am wondering why this code seems to loop infinitely? The logic, while not False = while True, and this True is referring to 100 < 0 which is false, hence it should execute the print statement instead, right? Why is it stuck in the loop then..?

num = 100
while not False:if num < 0:break
print('num is: ' + str(num))
Answer

a while statement should be proceeded with a conditional. The conditional True (or in your cause not False) always evaluates to True so the loop never ends.

the if block is never executed because because num < 0 never evaluates to True. Did you mean to decrement num by 1 in each iteration of the while block? If so, add a num = num - 1 within the while block:

num = 100
while not False:if num < 0:breaknum = num - 1
print('num is: ' + str(num))
https://en.xdnf.cn/q/120739.html

Related Q&A

sort a field in ascending order and delete the first and last number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question appears to be off-topic because it lacks sufficient information to diagnose the proble…

PermissionError: [Errno 13] Permission denied:

Im trying to write in a txt file the vertices of a spline mesh, but I get this error: PermissionError: [Errno 13] Permission denied: C\:Windows\system32\vt_84.txtThe code is

python calculator [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

How to sequence row based on another row?

I am trying to convert a formula from excel to pandas.The DataFrame looks like this: Column A Column B H H H J J J J K K I want to fill column B to increment while the value in co…

Multiclassification task using keras [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

Clarification needed regarding immutability of strings in Python [closed]

Closed. This question is seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. It does not meet Stack Overflow guidelines. It is not currently accepting …

Please help me in solving Fractional Knapsack problem (Maximum Value of the Loot)

Maximum Value of the LootProblem Introduction: A thief finds much more loot than his bag can fit. Help him to find the most valuable combination of items assuming that any fraction of a loot item can b…

Python countdown clock with GUI [duplicate]

This question already has an answer here:Making a countdown timer with Python and Tkinter?(1 answer)Closed 8 years ago.Im having problems with a countdown clock that I was making in Python for a Raspb…

Confidence calculation in association rule [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

How to write the names that start with A - L to one file and the rest to another?

Hello my assignment is :Create a system that allows the user to enter their name, title, surname, Dob, email and phone number. Once details are submitted, they should be written to a file. Surnames tha…