最近上完python课,老师让用python中的字典写一个猜单词游戏,于是就自己写了一个代码,可用于自己背单词,如果觉得我写的单词太简单,可以改成自己想要背诵的单词。
代码可能存在瑕疵,还请大佬指教!!
<code>import
random D1=dict(crawled='爬行'
,stimulate='刺激'
,difficult='困难'
,answer='回答'
,derive='得到'
,phone='手机'
,game='游戏'
,hello='你好'
,economic='经济'
,protocol='礼仪'
) D2=dict(爬行='crawled'
,刺激='stimulate'
,困难='difficult'
,回答='answer'
,得到='derive'
,手机='phone'
,游戏='game'
,你好='hello'
,经济='economic'
,礼仪='protocol'
) print(""" 欢迎参加猜单词游戏 根据汉语猜测单词,或者根据单词猜测汉语意思. """
) x='y'
while
x=='y'
or
x=='Y'
: print("本游戏有如下两种规则:\n"
) print("1:根据汉语猜测单词\n"
) print("2:根据单词猜测汉语\n"
) print("请输入你的选择:"
) a=int(input())if
a==1
: iscontinue="y"
while
iscontinue=="y"
or
iscontinue=="Y"
: word=random.choice(list(D1.values())) print("随机生成汉语意思为:"
, word) guess = input("\n请你猜满足该汉语意思的单词: "
)while
guess != D2[word]and
guess !=""
: print("对不起不正确."
) guess = input("继续猜: "
)if
guess == D2[word]: print("真棒,你猜对了!\n"
) iscontinue=input("\n\n是否继续(Y/N):"
)elif
a==2
: iscontinue="y"
while
iscontinue=="y"
or
iscontinue=="Y"
: word=random.choice(list(D1.keys())) print("随机生成单词为:"
, word) guess = input("\n请你猜给定单词的汉语意思: "
)while
guess != D1[word]and
guess !=""
: print("对不起不正确."
) guess = input("继续猜: "
)if
guess == D1[word]: print("真棒,你猜对了!\n"
) iscontinue=input("\n\n是否继续(Y/N):"
)else
: x=input("输入不合法是否重新输入(Y/N):"
)/<code>
關鍵字: while 汉语 iscontinue