python 构建类_使用Python中的类构建抽认卡的程序

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 构建类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值