#创建字典
geek ={"404":"clueless.From the web error message 404,meaning page not found.","Gooling":"searching the Internet for background information on a person.","keyboard Plaque":"the collection of debris found in computer keyboard.","Link Rot":"the process by which web page links become obsolete.","Percussive Maintenance":"the act of striking an electronic device to make it work.","Uninstalled":"being friend.Especially popular during the dot-bomb era."}#访问字典的值
geek["404"]if"Dancing Baloney"in geek:print("I known what Dancing Baloney is.")else:print("I have no idea what Dancing Baloney is.")print(geek.get("Dancing Baloney","I have no idea."))#键不存在,返回默认值“I have no idea"print(geek.get("Dancing Baloney"))
I have no idea what Dancing Baloney is.
I have no idea.
None
geek ={"lzq":"99","lfp":"96","lkp":"92","mjy":"94"}print(geek.items())
choice =Nonewhile choice !="0":print("""
Geek Translator
0-Quit
1-Look Up a Geek Term
2-Add a Geek Term
3-Redefine a Geek Term
4-Delete a Geek Term""")
choice =input("Choice:")if choice =="0":print("Bye")elif choice =="1":
term =input("What term do you want me to translate?")if term in geek:
definition = geek[term]print("%s means %s"%(term,definition))else:print("Sorry,I don't known",term)elif choice =="2":
term =input("What term do you want me to add?")if term notin geek:
definition =input("What's the definition?")
geek[term]= definition
print(term,"has been added.",geek)else:print("That term already exists!")elif choice =="3":
term =input("What term do you want me to redefine?")if term in geek:
definition =input("What's the new definition?")
geek[term]= definition
print(term,"has been refinined.",geek)else:print("That term doesn't exist!")elif choice =="4":
term =input("What term do you want me to delete?")if term in geek:del geek[term]print("I deleted",term)else:print("That's no term!")else:print("no this choice!")
dict_items([('lzq', '99'), ('lfp', '96'), ('lkp', '92'), ('mjy', '94')])
Geek Translator
0-Quit
1-Look Up a Geek Term
2-Add a Geek Term
3-Redefine a Geek Term
4-Delete a Geek Term
Choice:2
What term do you want me to add?whq
What's the definition?90
whq has been added. {'lzq': '99', 'lfp': '96', 'lkp': '92', 'mjy': '94', 'whq': '90'}
Geek Translator
0-Quit
1-Look Up a Geek Term
2-Add a Geek Term
3-Redefine a Geek Term
4-Delete a Geek Term
Choice:0
Bye
#游戏import random
HANGMAN =("""
-------
| |
|
|
|
|
|
|
|
|
-------------
""","""
--------
| |
| 0
|
|
|
|
|
|
|
|
--------------
""","""
---------
| |
| 0
| -+-
|
|
|
|
|
|
|
|
-----------------
""","""
----------
| |
| 0
| /-+-
|
|
|
|
|
|
|
|
-----------------
""","""
----------
| |
| 0
| /-+-/
|
|
|
|
|
|
|
|
-----------------
""","""
----------
| |
| 0
| /-+-/
| |
|
|
|
|
|
|
|
-----------------
""","""
----------
| |
| 0
| /-+-/
| |
| |
| |
| |
|
|
|
|
-----------------
""","""
----------
| |
| 0
| /-+-/
| |
| |
| | |
| | |
|
|
|
|
-----------------
""")print(type(HANGMAN))
MAX_WRONG =len(HANGMAN)-1
WORDS =("OVERUSED","CLAM","GUAM","TAFFETA","PYTHON")
word = random.choice(WORDS)# 随机一个单词
so_far ="-"*len(word)#每条横线表示word的一个字母
wrong =0# 记录猜错次数
used =[]#记录已经猜过的字母print("Welcome to Hangma.Good lucky.")while wrong < MAX_WRONG and so_far != word:print(HANGMAN[wrong])print("\nYou've used the follwing letters:\n",used)print("\nSo far,the word is:\n",so_far)#猜测环节
guess =input("\n\nEnter your guess:")
guess = guess.upper()while guess in used:print("You've already guessed the letter",guess)
guess =input("Enter your guess:")
guess = guess.upper()
used.append(guess)#判断环节if guess in word:print("\nYes!",guess,"is in the word!")#更新so_far
new =""for i inrange(len(word)):if guess == word[i]:
new += guess
else:
new += so_far[i]
so_far = new
else:print("\nSorry",guess,"is not in the word")
wrong +=1if wrong == MAX_WRONG:print(HANGMA[wrong])print("\n You've been hanged!")else:print("\nYou guessed it!")print("\nThe word was",word)
<class 'tuple'>
Welcome to Hangma.Good lucky.
-------
| |
|
|
|
|
|
|
|
|
-------------
You've used the follwing letters:
[]
So far,the word is:
------
Enter your guess:p
Yes! P is in the word!
-------
| |
|
|
|
|
|
|
|
|
-------------
You've used the follwing letters:
['P']
So far,the word is:
P-----
Enter your guess:b
Sorry B is not in the word
--------
| |
| 0
|
|
|
|
|
|
|
|
--------------
You've used the follwing letters:
['P', 'B']
So far,the word is:
P-----
Enter your guess:y
Yes! Y is in the word!
--------
| |
| 0
|
|
|
|
|
|
|
|
--------------
You've used the follwing letters:
['P', 'B', 'Y']
So far,the word is:
PY----
Enter your guess:q
Sorry Q is not in the word
---------
| |
| 0
| -+-
|
|
|
|
|
|
|
|
-----------------
You've used the follwing letters:
['P', 'B', 'Y', 'Q']
So far,the word is:
PY----
Enter your guess:t
Yes! T is in the word!
---------
| |
| 0
| -+-
|
|
|
|
|
|
|
|
-----------------
You've used the follwing letters:
['P', 'B', 'Y', 'Q', 'T']
So far,the word is:
PYT---
Enter your guess:h
Yes! H is in the word!
---------
| |
| 0
| -+-
|
|
|
|
|
|
|
|
-----------------
You've used the follwing letters:
['P', 'B', 'Y', 'Q', 'T', 'H']
So far,the word is:
PYTH--
Enter your guess:o
Yes! O is in the word!
---------
| |
| 0
| -+-
|
|
|
|
|
|
|
|
-----------------
You've used the follwing letters:
['P', 'B', 'Y', 'Q', 'T', 'H', 'O']
So far,the word is:
PYTHO-
Enter your guess:n
Yes! N is in the word!
You guessed it!
The word was PYTHON