The Student Room Group

help: How to loop back to while - Python

How can I make Python loop back to the beginning of my while loop.
I tried using break etc but it comes up as errors.
(edited 7 years ago)
Original post by jackymang12345
How can I make Python loop back to the beginning of my while loop.
I tried using break etc but it comes up as errors.


Can you post your code and what you have tried?
(edited 7 years ago)
^ this. Python will continue running the content within the while loop until the argument specified at the beginning of the loop evaluates as False. There is no need to pass to the beginning.

@jackymang12345 Are you referring to a for loop, where it would be necessary to pass onto the next iteration?
(edited 7 years ago)
Ok, so i should put a false statement right at the top. And then when it comes to the end of the coding under my while loop, do i use the break function?

Also, no i'm not using a for loop.

I can't post my coding, because its coursework.
Original post by jackymang12345
Ok, so i should put a false statement right at the top. And then when it comes to the end of the coding under my while loop, do i use the break function?

Also, no i'm not using a for loop.

I can't post my coding, because its coursework.


No, that's not what I said. To break from a while loop, either invalidate your initial statement:

Example:
g = 1;condition = True #condition may substituted with any argument, and doesn't have to be defined out of the while statement.
while g == 1: #g == 1 is True, therefore it runs the while loop.
if condition: g=2 # g==1 is now False, therefore it breaks out of the while loop
Or using "break", it will produce identical results.
g = 1;condition = True
while g == 1:
if condition: break
Can you quote the exact error that is raised?
(edited 7 years ago)
Ok, sorry not while loop, so this coding, for the else statement, how do i loop back to the print statement : print("code??") so that it can start all over again
from math import ceil
import sys

print("code??":wink:
lengthnumber =input("":wink:
incorrectnumber="0000000"

if lengthnumber==incorrectnumber:
sys.trackbacklimit=0
print("incorrect":wink:

else:
print("code correct":wink:
print(lengthnumber)
(edited 7 years ago)
Original post by jackymang12345
Ok, sorry not while loop, so this coding, for the else statement, how do i loop back to the print statement : print("code??":wink: so that it can start all over again
from math import ceil
import sys

print("code??":wink:
lengthnumber =input("":wink:
incorrectnumber="0000000"

if lengthnumber==incorrectnumber:
sys.trackbacklimit=0
print("incorrect":wink:

else:
print("code correct":wink:
print(lengthnumber)


You can eliminate the import "ceil", as I don't see you using it within your code.

The function "trackbacklimit" does not exist within the sys library, are you sure you didn't mean to call another function?

The most sensible thing would be to use a while loop.

I do have code written, but I'd like you to have a go (as it's homework):

Try:

1.

Creating a variable with your value (ie. 0000000)

2.

Creating a while loop that checks if the variable is equal to "0000000"

3.

Setting the variable to a user's input.

4.

Handling the printing of the terms "Incorrect", or "Correct" with if/else statements.

If the new value of the variable violates the constraint presented, ie is "correct", within the while statement, it will break after finishing the current iteration.
(edited 7 years ago)
I still don't get this. I tried this but it still won't allow the user to enter the code they want in python again if they entered it wrong the first time.

from math import ceil
import sys
print("code??")
lengthnumber =input("")
incorrectnumber="0000000"

while incorrectnumber==False:
print("incorrect")
else:
print("code correct")
print(lengthnumber)
(edited 7 years ago)
ok, so i tried this, because i am a beginner i find it very difficult. Thanks for your help. Still not allowing the user to type in code, even though i set the length_number to true when the user enters the wrong code, i did this so they are able to type something in again, but its not showing on the screen.

while length_number==True:
print("code?")
length_number =input("")
incorrect_number="0000000"

if incorrect_number==True:
print("incorrect")
length_number==True

else: incorrect_number==False
print("code")
(edited 7 years ago)
This. To get a boolean return (apart from the variables themselves being boolean), you'll need to add a comparison, ie. !=, ==, >, <, in, etc. I'm not sure what you're trying to achieve there, maybe you want to say (incorrectnumber==lengthnumber) == False, in which case you should say incorrectnumber != lengthnumber.
Original post by jackymang12345
ok, so i tried this, because i am a beginner i find it very difficult. Thanks for your help. Still not allowing the user to type in code, even though i set the length_number to true when the user enters the wrong code, i did this so they are able to type something in again, but its not showing on the screen.

while length_number==True:
print("code?":wink:
length_number =input("":wink:
incorrect_number="0000000"

if incorrect_number==True:
print("incorrect":wink:
length_number==True

else: incorrect_number==False
print("code":wink:


You're mismatching datatypes. incorrect_number is a string and therefore cannot have a value of False/True. You will need to do a comparison, ie. in, <, >, ==, != etc., for a boolean return from a line.
What exactly are you trying to do in your program? I've looked at your code briefly but don't think it makes any sense?

Quick Reply

Latest