gpt4 book ai didi

python - 没有命名的模块(导入错误 Python 2.7)

转载 作者:太空宇宙 更新时间:2023-11-04 01:15:45 24 4
gpt4 key购买 nike

我正在尝试按照每个步骤解决 Learn Python The Hard Way 中的练习 25,但我不断收到此错误:

>>> import ex25
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named ex25

我将本书网站上的代码复制粘贴到 Gedit(Linux 文本编辑器),但它似乎不是错字。

   def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words

def sort_words(words):
"""Sorts the words."""
return sorted(words)

def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print word

def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word

def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)

def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)

def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)

最佳答案

@Padraic Cunningham 和@James Mills 提出了正确的问题,您只是遇到了导入错误,没有任何暗示是代码错误。您需要确保该文件存在于您正在使用的目录中。请记住它区分大小写。

Learnpython.org Examples , Python.org Modules Docs

关于python - 没有命名的模块(导入错误 Python 2.7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24839131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com