Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python IDLE中编写和运行多个代码?_Python_Python Idle - Fatal编程技术网

如何在Python IDLE中编写和运行多个代码?

如何在Python IDLE中编写和运行多个代码?,python,python-idle,Python,Python Idle,如何让代码(见下文)在空闲状态下运行?我现在陷入了困境,找不到解释。 我知道如何在空闲状态下运行,只需一个“def”或代码,按F5键,例如写入。 在shell中输入字符串('udacity',3),结果。在这种代码中,如果有多个代码,它当然不会工作。为了更深入地理解,我想在Python在线教程中运行它,条件是它将在空闲状态下工作,反之亦然。 此外,我想知道,为什么输入#print hashtable_get_bucket(table,“Zoe”) 结果如下:#>>[[Bill',17],[Zoe

如何让代码(见下文)在空闲状态下运行?我现在陷入了困境,找不到解释。 我知道如何在空闲状态下运行,只需一个“def”或代码,按F5键,例如写入。 在shell中输入字符串('udacity',3),结果。在这种代码中,如果有多个代码,它当然不会工作。为了更深入地理解,我想在Python在线教程中运行它,条件是它将在空闲状态下工作,反之亦然。 此外,我想知道,为什么输入#print hashtable_get_bucket(table,“Zoe”) 结果如下:#>>[[Bill',17],[Zoe',14]]为什么“Bill',17”会出现在列表中

# Define a procedure, hashtable_get_bucket,
# that takes two inputs - a hashtable, and
# a keyword, and returns the bucket where the
# keyword could occur.

def hashtable_get_bucket(htable,keyword):
     return htable[hash_string(keyword,len(htable))]


def hash_string(keyword,buckets):
     out = 0
     for s in keyword:
        out = (out + ord(s)) % buckets
     return out

def make_hashtable(nbuckets):
     table = []
     for unused in range(0,nbuckets):
        table.append([])
     return table


#table = [[['Francis', 13], ['Ellis', 11]], [], [['Bill', 17],
#['Zoe', 14]], [['Coach', 4]], [['Louis', 29], ['Rochelle', 4], ['Nick', 2]]]

#print hashtable_get_bucket(table, "Zoe")
#>>> [['Bill', 17], ['Zoe', 14]]

#print hashtable_get_bucket(table, "Brick")
#>>> []

#print hashtable_get_bucket(table, "Lilith")
#>>> [['Louis', 29], ['Rochelle', 4], ['Nick', 2]]

谢谢你花时间阅读这篇文章,也谢谢你的建议

首先回答您的最后一个问题:
表是一个三重列表,因此列表列表包含列表。其中一个列表是
[['Bill',17],'Zoe',14]]
。由于
hash\u string
返回一个索引,因此它从
中检索一个列表,该列表恰好包含Bill和Zoe

此外,要从IDLE中运行多个函数,必须创建一个调用其他函数的新函数(新的
def my_assignment
或其他函数)


希望这能有所帮助。

请描述您的期望以及函数应该做什么。这是MIT 6.00x的吗?@Tichodroma:请参考代码的最后一部分(#…)。例如,打印hashtable\u get_bucket(table,“Zoe”)#>>>[[Bill',17],[Zoe',14]]@padraiccningham:CS101,这是“计算机科学入门”。。Udacity,第5课,查找存储桶等等。这个问题是关于如何编写一个具有多个函数的Python程序。它与Idle无关,如果使用其他编辑器编写程序也一样。您将如何编写新函数,以便调用提到的其他函数?您可能希望再次运行Python教程,特别是关于定义函数的部分,