下面的short()功能不起作用。full()作業得很好。有沒有解決方案,還是我必須創建另一個交換鍵和值的目錄?
import random
elements = {"Sc":"Scandium",
"Ti":"Titanium",
"V":"Vanadium",
"Cr":"Chromium",
"Mn":"Manganum",
"Fe":"Ferrum"}
def short():
question = random.choice(list(elements.values()))
print(question)
answer = input("What is the short name of this element?: ")
if answer == elements[question]:
print("Right")
def full():
question = random.choice(list(elements.keys()))
print(question)
answer = input("What is the full name of this element?: ")
if answer == elements[question]:
print("Right")
mode = input("Do you want to guess the short or full name? (sh/fu): ").lower()
if mode == "sh":
short()
elif mode == "fu":
full()
uj5u.com熱心網友回復:
我改變主意了。你不需要 reverse dict。
只是結構short()略有不同:
import random
elements = {"Sc":"Scandium",
"Ti":"Titanium",
"V":"Vanadium",
"Cr":"Chromium",
"Mn":"Manganum",
"Fe":"Ferrum"}
def short():
question = random.choice(list(elements.keys()))
print(elements[question])
answer = input("What is the short name of this element?: ")
if answer == question:
print("Right")
def full():
question = random.choice(list(elements.keys()))
print(question)
answer = input("What is the full name of this element?: ")
if answer == elements[question]:
print("Right")
mode = input("Do you want to guess the short or full name? (sh/fu): ").lower()
if mode == "sh":
short()
elif mode == "fu":
full()
short()現在像 一樣隨機選擇一個鍵full(),但根據在向前方向上易于訪問的長名稱提出問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/437561.html
