Hey! Sign in to get help with your study questionsNew here? Join for free to post

Python code help with for loop!

Announcements Posted on
TSR's new app is coming! Sign up here to try it first >> 17-10-2016
    • Thread Starter

    This isn't working !!! Please help!

    y=['yes' , 'yeah' , 'yep' , 'y' , 'yup']
    n=['no' , 'nah' , 'n', 'nope']
    i = 0
    def work():
    ....a=input("Is this correct?" )
    ....if a in y:
    ........print("Have a great day,Good bye!" )
    ....elif a in n:
    ........print("We are sorry." )
    ........for i in range(3):
    if i >= 3 :
    ....print("bye!!" ) ##* look below
    ....raise SystemExit

    ##* So say for example if the user inputs 'dhjgas' instead of y or n, for loop should basically make sure, that the function ('is this correct?' ) is printed 3 times.

    I want the system to automatically exit if the user inputs something other than 'y' or 'n' for more than 3 times. This is where the problem is. Once i enter the incorrect input for over 3 times , the program still continues and 'is this correct' repeats over and over again. Here is an example of what i DONT want the code to if i run it:
    is this correct? hfg
    is this correct?jhg
    is this correct?hgj
    is this correct?hgf
    ...# it keeps going on until i enter y or n.

    here is an example of what i want the program to do?=:
    is this correct? hfg
    is this correct?jhg
    is this correct?hgjbye!
    # user has entered wrong input 3 times so the programs exits

    I'm really sorry, if this is really confusing and thanks in advance.

    A while loop might cut it...
    x=3 #threshold for number of input times
    while i<x:
    .a=str(raw_input("Is this correct?"
    .if a in y:
    ..print("Have a great day. Good bye!"
    .elif a in n:
    ..print("We are sorry."
    .print("Bye!!" )
    .raise SystemExit

    Let me know if it works for you! (. is 1 indent, .. is 2)

    I've never done Python, but wanted to see if I could use an iPhone compiler with the code & start learning :-)

    y=['yes' , 'yeah' , 'yep' , 'y' , 'yup']
    n=['no' , 'nah' , 'n', 'nope']
    def work():
    a = input ("Is this correct?")
    if a=='y':
    print("Have a great day! Good bye!")
    elif a=='n':
    print("We are sorry")
    for i in range(3):
    if i >= 3 :

    For some reason it only registers the 'y' & 'n' input, not the rest, plus I don't know how to stop the loop once someone chooses a right input. (Never done this before!)


    # y = ["yes", "y"]
    n = ["no", "n"]

    def work():
    ....trial = 0
    ....# Loop to continue running as long as tries not up to three
    ....while trial < 3 :
    ........answer = input("Enter your choice: ")

    ........# break ends the loop
    ........if answer in y:
    ............print("Have a good day, Good bye!")

    ........elif answer in n:
    ............print("We are sorry!")

    ........# This is your main part
    ............print("Is this correct?")
    ............ #trial +=1 is the same as trial = trial+1. The aim is to increase trial by 1 if wrong input is typed
    ............trial += 1

    # Calling function
Write a reply…


Submit reply


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: April 8, 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.

Do you like sleeping in a cold room?
Useful resources

Study tools


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.


Make study resources

Create all the resources you need to get the grades.


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.


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

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