【发布时间】:2014-03-05 20:08:13
【问题描述】:
当我在 IDLE 中运行我的代码时,我的代码工作正常,但是当我在 CMD 中正常打开它时,它会显示一条错误消息并立即关闭,让我没有时间阅读错误消息。然而,我确实设法截取了错误消息,它说:
“文件”文件位置...“第 12 行,在编码中 返回 codecs.charmap_encode(输入,错误,encoding_map) UnicodeEncodeError:“charmap”编解码器无法在位置 102 编码字符“\u03c0”:字符映射到“
我不确定这意味着什么以及如何修复它,奇怪的是它在 IDLE 中有效但在 CMD 中无效。 CMD 和 IDLE 都在同一版本的 python 上,我的计算机上只安装了 1 个版本的 python,所以问题不在于版本不匹配。你知道它为什么工作然后不工作以及如何修复它吗?我使用的是 python 3.3.3。这是我的代码(它用于使用无限系列近似计算 Pi,用户输入他们想要使用的系列的多少项):
import math
go="Y"
while go=="Y" or go=="y" or go=="Yes" or go=="yes":
count=input("The infinite series 1/1 - 1/3 + 1/5 - 1/7... converges to π/4, how many of these divisions would you like the computer to do?\n")
if count.isdigit():
count = int(count)
pi=0
realcount = 0
while realcount <= count:
realcount = realcount + 1
if realcount / 2 == math.floor(realcount / 2):
pi = pi - (1 / ((2 * realcount) - 1))
else:
pi = pi + (1 / ((2 * realcount) - 1))
print("π ≈",4 * (pi))
go=input("Would you like to try again?\n")
else:
go=input("Sorry you must input a positive integer, would you like to try again?\n")
【问题讨论】:
-
最有可能是 PI 字符。尝试写出 PI,看看是否有帮助。就是它们,或者是您拥有
print("π ≈",4 * (pi))的那个“等价”字符。很可能控制台使用的编码不能正确处理这些字符。 -
附加提示:while 条件可以写成
while go.lower() in ('y', 'yes'):。 -
谢谢你解决了我的问题。
标签: python cmd python-idle