【发布时间】:2017-02-14 07:21:36
【问题描述】:
我刚刚学习了 Python 的基础知识,在谷歌搜索与 Python 相关的很酷的事情时,发现了这个 pdf:Binary_Image (将 1/0 转换为 * 和空格)这个 pdf 有一个挑战部分,它说的是
"修改您的程序,使其显示宽度为 6 个字符。您可以创建一个新变量 称为“位置”,并为用户输入的每个字符加1,每次打印一个“换行符” 位置达到6。”
我对挑战的理解是创建一个变量来计算 img_out 然后每 6 个字符创建一个换行符
我不明白如何使用“位置”变量,所以我尝试了这段代码
#get a binary number from the user
img_in = input("Enter your b&w bitmap image :")
#initially, there is no output
img_out = ""
#loop through each character in the binary input
for character in img_in:
#add a star(*) to the output if a 1 is found
if character == "1":
img_out = img_out + "*"
#otherwise, add a space
else:
img_out = img_out + " "
#count the img_out
if len(img_out) >= "7":
img_out = img_out + "\n"
else:
img_out = img_out
#print the image to the screen
print(img_out)
当使用cmd /k 编译代码时使用python path/to/file.py
Enter your b&w bitmap image :11111101101101
Traceback (most recent call last):
File "C:\Users\saber\Desktop\testing.py", l
if len(img_out) >= "7":
TypeError: unorderable types: int() >= str()
如果有人可以帮助我解决这个问题,那就太好了 提前致谢
P.S : 我在 Windows 上使用 Python 3.5.2
【问题讨论】: