我已經使用python製作了一個簡單的字典程序。Python:調用函數內部時發生錯誤
dictionary = {"testword" : "testmeaning"}
y = ["y", "Yes", "Y", "YES", "yes", "yep", "YEP", "Yep"]
n = ["n", "No", "N", "no", "NO", "nope", "Nope", "NOPE"]
def ans():
ans = raw_input("Y/N")
if ans in y:
meaning = raw_input("Type the meaning.")
dictionary[x] = meaning
print "Added!. Thanks for using the dictionary."
reload
elif ans in n:
print "Thanks for using the dictionary."
else:
print "Type either Y or N."
ans()
def add_word():
print "Do you want to add it?"
ans()
def close_ans():
close_ans = raw_input("Y/N")
if close_ans in y:
quit
elif close_ans in n:
in_dict()
else:
print "Type either Y or N"
close_ans()
def close():
print "Close?"
close_ans()
def in_dict():
global x
x = raw_input("Type the word.")
if x in dictionary:
print dictionary[x]
close()
else:
print "This word isn't in the dictionary yet."
add_word()
close()
in_dict()
該代碼是非常明顯的。我用一個名爲「testword」的詞做了一個字典,其值爲「testmeaning」。還有一個正面和負面答案的列表,將用於確定用戶在某些情況下是否同意或拒絕。它首先調用函數in_dict,要求用戶輸入單詞。如果單詞在詞典中,則它打印含義並要求關閉。如果不是,它詢問用戶是否想要添加它。如果你不理解它,再次查看代碼。如果用戶輸入其他的東西,然後再輸入列表y或n中的單詞,它會再次提問。但是,它不起作用。如果我在y和n中輸入除單詞以外的任何內容,則會顯示以下錯誤。
Traceback (most recent call last):
File "C:\Users\Prateek\Desktop\dictionary.py", line 46, in <module>
in_dict()
File "C:\Users\Prateek\Desktop\dictionary.py", line 43, in in_dict
add_word()
File "C:\Users\Prateek\Desktop\dictionary.py", line 20, in add_word
ans()
File "C:\Users\Prateek\Desktop\dictionary.py", line 16, in ans
ans()
TypeError: 'str' object is not callable
用'ans = raw_input(「Y/N」)覆蓋函數名稱' –