Resetting a while True loop - python-3.x

How do you reset a while True loop. For example this is my code:
x=True
while x==True:
print ("Random Text")
randomtext= input()
if randomtext == "yes":
print ("hi")
x=False
elif randomtext == "no":
print("This is supposed to reset the loop")
#resets loop and prints Random Text again
print ("hi")
if x==True:
print ("placeholder text")
input()
I want it to reset the loop if "randomtext" is equal to yes. I want to reset it it in the middle of the loop. This is a very simple question but this is getting in my programming. Thank you.

I'm assuming that by resetting the loop you mean jumping code until reaching the start of the loop again. This is done with the "continue" keyword.
if randomtext == "yes":
continue
If you actually meant breaking out of the loop, you should instead use the "break" keyword.

Related

I am trying to tell the program to display insufficient input. unless those 5 are selected

while True:
if users_choice!= ('1','2','3','4','5'):
print ('Insufficient input Method')
break
This is inside my main loop. i dont want to place and if for each one.
Use users_choice not in ('1','2','3','4','5') instead.
while True:
if users_choice not in ('1','2','3','4','5'):
print ('Insufficient input Method')
break
Operator != is actually trying to compare a string with tuple. That's why != wont work here.
To understand clearly, test this code
users_choice = '0'
while True:
print(type(users_choice))
print(type(('1','2','3','4','5')))
if users_choice not in ('1','2','3','4','5'):
print ('Insufficient input Method')
break

how can I start a new input() instance after breaking the initial input() with EOF exception?

I need to take multiple line input from user - works with a while loop, but have an issue starting another input() as the EOF gets "passed" over to the new input()
I've tried using a combination of the sys stdin, and func() but, not sure why it's happening.
while True:
try:
list = input()
except EOFError:
break
input('input2:')
1) Don't use list as a variable name. That is a reserved word in python.
2) You can catch a KeyboardInterrupt which will stop the loop after a Ctrl + C and EOFError which catches a Ctrl + D:
while True:
try:
list = input()
except (EOFError, KeyboardInterrupt):
break
input('input2:')
Alternatively you can prime your loop and let the loop exit on a set condition:
my_input = input()
while my_input: # Break if nothing was inputted
print(f"Inputed: {my_input}")
my_input = input()
input('input2:')

Making a conditional loop

I'm making a guessing game or computer science in school where the number to guess is seven. I have tried using while loops and if elif else statements but it doesn't seem to want to make a conditional loop My code is as follows:
guess=int(input("Guess a number!"))
var=1
while var==1:
if guess !=7:
print("Try again")
else:
print("Well done")
Any help would be appreciated thanks. I need it in about a week and a half's time.
If you're trying to allow your player to continuously guess the input needs to be at the top of the while loop, before the conditional-branch
while(True):
guess = input("Make a guess: ")
if(guess == 7):
print(guess,"was correct!")
break
else:
print("Nope. Guess again.")
Of course, you could make it more interesting in a variety of ways.
guess=int(input("Guess a number!"))
var=1
while var==1:
if guess !=7:
print("Try again")
guess=int(input("Guess a number!"))
else:
print("Well done")
var=0 #set var to 0, to exit the loop
Try this. You need to exit the loop, and to do that, var needs to be set to 0.

Not breaking out of loop

Relative newbie to Python here. My program at this point is waiting for the user to press enter so it can get back to the main loop. Is something wrong with the following code? (Ignore the print statements, I am using them for debugging.)
def checkReturnKeyPress():
print ('check return key function started')
while True: # loop until user presses return key
print ('check return key 2nd loop')
for event in pygame.event.get(): # event handling loop
print ('keydown for loop')
if event.type == KEYDOWN:
print ('keydown')
if event.key == K_RETURN:
print ('return')
return
Sorry again for how messy this is, I'll clean it up once it's working. I have a feeling the part that's wrong is the "for event in pygame" part, if that helps.
You need a break in there somewhere. I'm guessing the K_RETURN is where you are looking for the return key to be pressed. If so you should add a break after the print('return') so that you will break out of your infinite loop.

Scopes within while loops

from random import randint
isRunning =True
while isRunning:
dice1 = randint(1,7)
dice2 = randint(1,7)
print("The first die landed on ℅d and the second landed on ℅d." ℅ (dice1,dice2))
user_input = input("Contiue? Yes or No?\n>")
if user_input == "Yes" or "yes":
print("="*16)
elif user_input == "No" or "no":
isRunning = False
I feel like I'm making such a simple mistake and when I decided to look into global variables and what not it still doesn't help. Could anyone explain why the while loop doesn't terminate, although the variable was set to false.
if user_input== "Yes" or "yes" should be
if user_input== "Yes" or user_input =="yes", alternatively it's equivalent to if any(user_input==keyword for keyword in["Yes", "yes"]):
your original if clause is splitting to
if user_input=="Yes" or if "yes" and if "yes" always true, therefore your if-elseif clause always go to if clause.

Resources