help: How to loop back to while - Python

Announcements
    • Thread Starter
    Offline

    2
    ReputationRep:
    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.
    Offline

    3
    ReputationRep:
    (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?
    Offline

    3
    ReputationRep:
    What do you mean "loop back to the beginning of your while loop"?

    You don't need to make it loop because by default that's how a loop works. As long as you write it correctly it will loop back without the need for any additional statements.

    Can you post the code in question? It's impossible to give you any help without knowing what your code looks like and what it's supposed to do
    Offline

    3
    ReputationRep:
    (Original post by Acsel)
    What do you mean "loop back to the beginning of your while loop"?

    You don't need to make it loop because by default that's how a loop works. As long as you write it correctly it will loop back.

    Can you post the code in question? It's impossible to give you any help without knowing what your code looks like and what it's supposed to do
    ^ 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?
    • Thread Starter
    Offline

    2
    ReputationRep:
    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.
    Offline

    3
    ReputationRep:
    (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:
    Code:
    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.
    Code:
    g = 1;condition = True
    while g == 1: 
       	if condition: break
    Can you quote the exact error that is raised?
    • Thread Starter
    Offline

    2
    ReputationRep:
    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
    Code:
    from math import ceil
    import sys 
    
    print("code??";)
    lengthnumber =input("";)
    incorrectnumber="0000000" 
    
    if lengthnumber==incorrectnumber:
     sys.trackbacklimit=0 
     print("incorrect";)
    
    else: 
    print("code correct";)
     print(lengthnumber)
    Offline

    3
    ReputationRep:
    (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??" so that it can start all over again
    Code:
    from math import ceil
    import sys 
    
    print("code??";)
    lengthnumber =input("";)
    incorrectnumber="0000000" 
    
    if lengthnumber==incorrectnumber:
     sys.trackbacklimit=0 
     print("incorrect";)
    
    else: 
    print("code correct";)
     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.
    Offline

    3
    ReputationRep:
    (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??" so that it can start all over again
    Code:
    from math import ceil
    import sys 
    
    print("code??";)
    lengthnumber =input("";)
    incorrectnumber="0000000" 
    
    if lengthnumber==incorrectnumber:
     sys.trackbacklimit=0 
     print("incorrect";)
    
    else: 
    print("code correct";)
     print(lengthnumber)
    As _gcx said.

    So you basically want to run the print("code") block, check the if statement and then loop back to run it all again? The simplest way is to chuck everything inside a loop. If you want the user to have control over how many times it loops then use a variable to control that. If not then use a loop that always runs (kind of bad practice at this stage)
    • Thread Starter
    Offline

    2
    ReputationRep:
    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)
    Offline

    3
    ReputationRep:
    (Original post by jackymang12345)
    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 ceilimport sys print("code??"lengthnumber =input(""incorrectnumber="0000000"while incorrectnumber==False: print("incorrect" ##else: print("code correct" print(lengthnumber)
    That didn't import as you want but it looks like you've put the while loop in the middle of your code. That's not the right place. The general structure of the code needs to be:

    While loop
    print code and input
    if statement

    If you want something to loop it needs to be inside the loop. Python isn't great for understanding this because you don't use brackets to signify where things begin and end.

    You'll also get weird errors because you've assigned the incorrectnumber variable to a string of 0000000 and your trying to check if it is equal to a boolean value of false.
    • Thread Starter
    Offline

    2
    ReputationRep:
    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")
    Offline

    3
    ReputationRep:
    (Original post by Acsel)
    That didn't import as you want but it looks like you've put the while loop in the middle of your code. That's not the right place. The general structure of the code needs to be:

    While loop
    print code and input
    if statement

    If you want something to loop it needs to be inside the loop. Python isn't great for understanding this because you don't use brackets to signify where things begin and end.

    You'll also get weird errors because you've assigned the incorrectnumber variable to a string of 0000000 and your trying to check if it is equal to a boolean value of false.
    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.
    Offline

    3
    ReputationRep:
    (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?"
    length_number =input(""
    incorrect_number="0000000"

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

    else: incorrect_number==False
    print("code"
    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.
    Offline

    3
    ReputationRep:
    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?
    Offline

    3
    ReputationRep:
    (Original post by surina16)
    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?
    This as well. I copy/pasted the code into a python editor and after cleaning it up so that it ran (there's some weird semi colon positioning in the one I copied) all it seems to do is take a user input and reply with code correct unless I type 0000000.
    I don't really see what the code is supposed to do in this scenario.

    To make it loop indefinitely all I had to do was create a variable, assign it a value, add a while loop before the print statement and indent appropriately. In terms of the actual code I added it was:

    i = 1

    while i==1:
    everything else indented here

    The code loops forever or you can add a user prompt to make it stop looping.
 
 
 
Write a reply… Reply
Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. Oops, you need to agree to our Ts&Cs to register
  2. Slide to join now Processing…

Updated: October 4, 2016
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Poll
Which is the best season?
Useful resources

Study tools

Rosette

Essay expert

Learn to write like a pro with our ultimate essay guide.

Thinking about uni already?

Thinking about uni already?

See where you can apply with our uni match tool

Student chat

Ask a question

Chat to other GCSE students and get your study questions answered.

Creating

Make study resources

Create all the resources you need to get the grades.

Planner

Create your own Study Plan

Organise all your homework and exams so you never miss another deadline.

Resources by subject

From flashcards to mind maps; there's everything you need for all of your GCSE subjects.

Papers

Find past papers

100s of GCSE past papers for all your subjects at your fingertips.

Help out other students

Can you help? Study help unanswered threads

Groups associated with this forum:

View associated groups

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Quick reply
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.