I have recently started creating a Python program for my coursework. I have pretty much the main skeleton of the program but I have created a while loop and I need it to stop when the user enters ten words/definitions. I have looked about on stackoverflow and everything I have tried hasn't really worked. Anyway, hopefully some helpful people can give help me out.
def teacher_enter_words():
done = False
print 'Hello, please can you enter a word and definition pair.'
while not done:
word = raw_input('\nEnter a word: ')
deff = raw_input('Enter the definition: ')
# append a tuple to the list so it can't be edited.
words.append((word, deff))
add_word = raw_input('Add another word? (y/n): ')
if add_word.lower() == 'n':
print "Thank you for using the Spelling Bee program! The word(s) and definition(s) will now appear when a student logs in.\n"
done = True
def enterWords(maxWords):
done = False
words = []
while len(words) < maxWords and not done:
word = raw_input('\nEnter a word: ')
deff = raw_input('Enter the definition: ')
words.append((word, deff))
add_word = raw_input('Add another word? (y/n): ')
if add_word.lower() == 'n':
print "Thank you for using the Spelling Bee program! The word(s) and definition(s) will now appear when a student logs in.\n"
done = True
return words
First you need to declare words
list currently.
You can check the size of words
using len()
so if you have 10 words you can exit the loop.
This can be done also without using done
variable, you can set the while loop condition to be len(words) < 10
so you can save the use of done
def teacher_enter_words():
done = False
print 'Hello, please can you enter a word and definition pair.'
words = []
while not done:
word = raw_input('\nEnter a word: ')
deff = raw_input('Enter the definition: ')
# append a tuple to the list so it can't be edited.
words.append((word, deff))
if len(words) == 10:
print "Hi you have 10 words!!!"
break
add_word = raw_input('Add another word? (y/n): ')
if add_word.lower() == 'n':
print "Thank you for using the Spelling Bee program! The word(s) and definition(s) will now appear when a student logs in.\n"
done = True
print words
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.