Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    10
    ReputationRep:
    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." )
    ....else:
    ........i=1
    ........for i in range(3):
    ............i=i+1
    ............work()
    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.
    Offline

    0
    ReputationRep:
    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."
    .else:
    ..i=i+1
    else:
    .print("Bye!!" )
    .raise SystemExit

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

    3
    ReputationRep:
    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']
    i=0
    def work():
    a = input ("Is this correct?")
    if a=='y':
    print("Have a great day! Good bye!")
    elif a=='n':
    print("We are sorry")
    else:
    i=1
    for i in range(3):
    i=i+1
    work()
    if i >= 3 :
    print("Bye!")

    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!)



    PlGLET
    Offline

    0
    ReputationRep:
    # 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!")
    ............break

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

    ........# This is your main part
    ........else:
    ............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
    work()
 
 
 
Poll
Do you agree with the PM's proposal to cut tuition fees for some courses?
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

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