Jump to content

while true and if statements

Enderg312

I am trying to loop my program with a while true but I have an if statement in there and if I type close it is suppose to close it and with the while true it wants to ignore the close part. How to I allow my program to loop and close when I type close? 

And it is python coding.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Enderg312 said:

I am trying to loop my program with a while true but I have an if statement in there and if I type close it is suppose to close it and with the while true it wants to ignore the close part. How to I allow my program to loop and close when I type close? 

And it is python coding.

You can use break to stop executing a loop, like e.g.:

while True:
	if(someconditionhere):
		break

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, WereCatf said:

You can use break to stop executing a loop, like e.g.:


while True:
	if(someconditionhere):
		break

 

Can you explain more because all that is doing is stopping the while True altogether?

Link to comment
Share on other sites

Link to post
Share on other sites

47 minutes ago, Enderg312 said:

Can you explain more because all that is doing is stopping the while True altogether?

Eternal loops can only be stopped if you apply a inner if/else statement. so like @WereCatfexplained:

inpt = input("What do you want? ")
while True:
    if inpt == "close":
        print("looped stop")
        break # You can also use sys.exit() to terminate the whole program
    else:
    	# If the input insnt close you dont want to lock the loop
        inpt = input("What do you want? ")

The else part is not "mandatory" but if you want to have always a way to trigger that if

Current Rig:

Mobo - MSI B450 Pro-Max VDH | CPU - Ryzen 5 3600 (AMD duh) | GPU - GTX 1660 Super (MSI) | WAM - 2 x 8GB 3200MHz (G.SKILLS Ripjaws)Storage - 1 x 256GB SSD (Crucial MX500) + 1 x 2TB | HDD 7200RPM (Seagate Barracuda) | PSU - Cooler Master 450W | Case - Cooler Master Q300L | Display - Asus VP248QG (Main) + AOC 24B1W1 | Other Stuff - Bloody B120 (Keyboard) + HP X220 Gaming Mouse + Asus A541UJ (Laptop)

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Emily123 said:

Why not just make a Boolean for your while loop and then set it to false when you want it to stop?

This is also my preferred method. In a simple program like this, it usually goes something like:

stopMe = False
while not stopMe:
  myVar = GetUserInput()
  if myVar == "stop":
    stopMe = True
    
  else if myVar == "go":
    MyGoFunc()
    
  else if myVar == "andOnandOn":
    MyOtherFuncs()
    
EndOfLoopFuncs()


The standard user input loop is:

  1. Get input
  2. Parse Input
  3. Operate on Input
  4. Goto Step #1

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×