Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top