【发布时间】:2013-11-21 23:01:09
【问题描述】:
我正在尝试将此文件制作成两个字典以通过用户的输入进行访问,但我完全迷失了。很遗憾,我已经为此工作了几个星期。我已经读取了文件,并且知道如何设置输入选项,而不是如何根据用户选项和名称来获取某个名称。
到目前为止,这是我的代码:
sonfather = {}
fatherson = {}
names = open('names.dat', 'r')
sonfather = names.read().split(',')
print sonfather
print "Father/Son Finder"
print "0 - Quit"
print "1 - Find a Father"
print "2 - Find a Grandfather"
print "3 - Find a Son"
print "4 - Find a Grandson"
print "5 - List of names"
control = ""
while control != "quit":
choice = input("Enter your choice here: ")
if choice == 0:
control = "quit"
elif choice == 1:
print input("Enter the name of the son :")
【问题讨论】:
-
仅仅通过这个就很难看出你想要完成什么。你能提供输入和预期输出吗?
-
输入会说用户想找一个父亲。然后将提示他/她输入儿子的名字。使用儿子的名字,它将返回父亲的名字。当我运行儿子的打印时,我得到: ['john:fred', 'fred:bill', 'sam:tony', 'jim:william', 'william:mark', 'krager:holdyn', 'danny:布雷特,'丹尼:伊萨克','丹尼:杰克','布拉森:扎德','大卫:迪特','亚当:塞斯','塞思:诺斯']
-
不是那个,文件输入。
-
我对你在问什么感到有点困惑。
-
我很困惑这个。哈哈
标签: python dictionary