Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

If I wanted my program to go back to a certain part of my program. How do I do that? Please help. Thanks a lot.

0 votes
asked Feb 20, 2020 by Arsen Crawford
For example;

There's a question and there's only 3 choices/answers. But you've entered something that is not on choices. Then a notification will pop up (ex. Invalid input. Please try again.) How do I return the answering part?

print('n1. What is print function?\n')
    print('A. used to receive data from keyboard in a program.')
    print('B. used to execute the codes.')
    print('C. used for declaration.\n')

    response = input('Your answer: ')

    if response == 'A' or response == 'a':
        print('\nAwesome, that is correct\n')
        score = score + 1
    elif response == 'B' or response == 'b' or response == 'C' or response == 'c':
        print('\nSadly, that is incorrect.\n')
    else:
        This part will go back to response part. How?

Thanks a lot.

2 Answers

0 votes
answered Feb 20, 2020 by Prashant

There are are several methods to do it:

  • Wrap a loop on your if-else function, e.g. while(1) and break on "a" and "b" options.
  • Use goto function.

Many more methods are there like loop with switch-case, etc., happy coding :)

0 votes
answered Feb 20, 2020 by Shubham

use Do While loop, this loop will execute atleast once, Here it will repeat until your answer doesn't matches A or a.

do {

/*code for response for input*/

response = input('Your answer: ');

}

while(response!='a' or response!= 'A');

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...