python 构建类
Flashcard is a virtual card that contains a word and its meaning and for each word, we define a separate flashcard. Python's class could help us in making such a type of program.
抽认卡是一种虚拟卡,其中包含一个单词及其含义,对于每个单词,我们定义一个单独的抽认卡。 Python的类可以帮助我们制作这种类型的程序。
Program:
程序:
# We are making a flash card program,
# in which each flashcard will consists
# of a word and its meaning
# Using class in python would make it easy
# We are defining a class 'card'
class card(object):
def __init__(self, word, meaning):
self.word = word
self.meaning = meaning
def getmeaning(self):
return self.meaning
def __str__(self):
return self.word + ' : ' + self.getmeaning()
# Defining a function for making a list of flashcards
def flashcard(flash, words, meanings):
flash.append(card(words, meanings))
return flash
lists = []
y = input("Want to add a flashcard? ")
while y == 'y' or y == 'Y':
word = input("Word you want to add to your Flashcards: ")
meaning = input("Meaning of the word in flashcard: ")
lists = flashcard(lists, word, meaning)
y = input("for adding another flashcard, press Y/y :")
print("----Following are the flashcards you made----")
for el in lists:
print(el)
Output
输出量
Want to add a flashcard? y
Word you want to add to your Flashcards: rescind
Meaning of the word in flashcard: cancel officially
for adding another flashcard, press Y/y :y
Word you want to add to your Flashcards: hegemony
Meaning of the word in flashcard: dominance over certain area
for adding another flashcard, press Y/y :y
Word you want to add to your Flashcards: brazen
Meaning of the word in flashcard: unrestrained by convention or proprierty
for adding another flashcard, press Y/y :n
----Following are the flashcards you made----
rescind : cancel officially
hegemony : dominance over certain area
brazen : unrestrained by convention or proprierty
翻译自: https://www.includehelp.com/python/build-flashcard-using-class.aspx
python 构建类