1

How do I repeat a function in python. Such as Repeat... Unil in other languages. Thanks, this is the code I'm trying to repeat:

import random


line = random.choice(keywords)
print('Keyword:')
print (line)

line = random.choice(definitions)
print ('A:')
print (line)

line = random.choice(definitions)
print ('B:')
print(line)

line = random.choice(definitions)
print ('C:')
print(line)

#randomly selects a keyword and definition from the file

A = []
ans = input('Is it A, B or C?')
print()

if ans == 'A' :
    print ('Correct')
else:
    print ('Incorrect')

#asks the user for an answer, then tells them if their correct or not

Any help would be much appreciated!

3
  • 1
    how many times? why didn't you google? Commented Feb 6, 2014 at 9:31
  • 6
    Python supports 'for' and 'while' Commented Feb 6, 2014 at 9:31
  • 1
    Not exactly clear what you're trying to do Commented Feb 6, 2014 at 9:34

2 Answers 2

1

To loop for infinity (or until a certain condition is met):

while True:
    # Insert code here
    if conditition_to_break:
        break

This will call your code in an endless loop until condition_to_break is True and then breaks out of the loop.

You can read more on while loops here.

If you want to repeat something n times try using a for loop (read more here).

for x in range(3):
    # This code will execute 3 times
    print("Executed {0} times".format(x+1))
Sign up to request clarification or add additional context in comments.

Comments

1

Did you mean like while or for? If you meant do while then it's a bit tricky in python, since python does not have a built-in function for it, but your can do this:

while(True):
   your_code_here()
   if(your_exit_term): break

I know it's ugly, but I'm not familiar with any other way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.