Skip to content
Advertisement

Tag: while-loop

Python Walrus Operator in While Loops

I’m trying to understand the walrus assignment operator. Classic while loop breaks when condition is reassigned to False within the loop. Why doesn’t this work using the walrus operator? It ignores the reassignment of x producing an infinite loop. Answer You seem to be under the impression that that assignment happens once before the loop is entered, but that isn’t

repeating a program using while loop failed

I coded a program that is meant to repeat itself over and over until the to_continue loop is broken by the input “N”. However, it didn’t seem to work. Output: whilst I meant for it to output Full program if that’s helpful: Any help would be deeply appreciated :) Please comment if any of the explanations are unclear or if

Should I be using an if statement or a loop?

It’s me, your friendly Python noob! I’m working on this program and I’m trying to figure out how to get the program to stop if the number exceeds a certain threshold OR if the user enters anything other than a number to loop back around and try again. Here is what I have so far: The if statements in main()

Python while loop not working as intended

I don’t know how to get over this problem with while loop. So basically I want to return the number of zeros at the end of a number’s factorial. After the while loop runs only 1 time, it breaks; I don’t know why. Help would be very appreciated. Thanks! Answer After multiplying your value by 0.1 it becomes a float,

Iteration numbers with True False condition

Please HELP!! I have a list: I want to generate iteration number if it’s False the number continues and if it’s True the number stays. I Try this code: it’s just keep running and did not show any output. the desired output will be [1,2,3,3,3,4,5,6,6] I have no idea what’s wrong with it Thanks Answer Yet another version of this

Using primitive operators to find factorial of N up to K depth

Having difficulty coming up with a solution using: iteration/control-flow and accumulation. More than just a solution, would prefer having an answer with hints and explanation. Answer Ignoring k=0 you have want to multiply k numbers starting with n and ending with n-k. The above loops k times and since i will increment by 1 starting from 0, you can simply

While loop spamming output instead of printing output once?

I’m making a regular guess the number game. I’m having problems when I enter the guess and the program spams lower or higher instead of just typing it once, since I’m using a while True loop. Here I’m just opening the CSV file and picking a random number. Here I’m importing numbers 1-50 and picking two random numbers which will

Advertisement