gpt4 book ai didi

python - Zed 的 Learn Python the Hard way 练习 25 中的 'import' key

转载 作者:太空宇宙 更新时间:2023-11-03 18:56:39 25 4
gpt4 key购买 nike

我在将代码导入 python 解释器 (powershell) 时遇到问题。我通过 powershell 打开 python,当我输入“import ex24”时,根本没有出现任何内容,这是使用我从他的网站复制并粘贴的代码(只是为了确定):

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)

当他执行它时,他得到:

>>> import ex25
>>> sentence = "All good things come to those who wait."
>>> words = ex25.break_words(sentence)
>>> words
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> sorted_words = ex25.sort_words(words)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> ex25.print_first_word(words)
All
>>> ex25.print_last_word(words)
wait.
>>> wrods
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'wrods' is not defined
>>> words
['good', 'things', 'come', 'to', 'those', 'who']
>>> ex25.print_first_word(sorted_words)
All
>>> ex25.print_last_word(sorted_words)
who
>>> sorted_words
['come', 'good', 'things', 'those', 'to', 'wait.']
>>> sorted_words = ex25.sort_sentence(sentence)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> ex25.print_first_and_last(sentence)
All
wait.
>>> ex25.print_first_and_last_sorted(sentence)
All
who

此外,当我像平常一样手动输入代码时,我收到此错误。我看不出我犯了什么错误:

import ex25 Traceback (most recent call last): File "", line 1, in File "ex25.py", line 1 SyntaxError: Non-ASCII character '\xff' in file ex25.py on line 1, but no encoding declared; eps/pep-0263.html for details

这是我的手册副本(ex25):

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)

最佳答案

看起来你的编辑正在输入 BOM在文件开始处。这些标记在许多编辑器中是不可见的。

BOM 在 UTF-8 中没有任何意义,因此只需将编辑器设置为保存为“不带 BOM 的 unicode” 或同等内容(应位于 settings 中的某个位置>首选项)。

The byte order mark (BOM) is a Unicode character used to signal the endianness (byte order) of a text file or stream. It is encoded at U+FEFF byte order mark (BOM). BOM use is optional, and, if used, should appear at the start of the text stream.

在 UTF-16 或 UTF-32 中,16 或 32 位单元可以用 big-endian 表示。或little-endian字节顺序,取决于平台。

由于 UTF-8 以字节存储,并且字节在每个平台上都是相同的,因此表明字节顺序是没有用的。为什么 Unicode 标准允许 UTF-8 中的 BOM - 最糟糕的是,为什么有些编辑器在不需要或不推荐的情况下会这样做,这超出了我的能力范围(愚蠢和愚蠢)。

关于python - Zed 的 Learn Python the Hard way 练习 25 中的 'import' key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17119200/

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