input() 语句不断引发 TypeError
input() Statement Constantly Raising TypeError
我做了一个 class,它使用了 PIL library
中 Image
模块中的一些 methods/functions(我不知道怎么称呼它们)。在此代码中,我要求用户输入要调整到的图像的新高度。因为我希望用户在出现错误时再次输入它,所以我将它放在 while
循环中。
我最初试图接受一个元组,然后将其解压缩到 new_height 和 new_width 变量中,但我认为这可能会使用户感到困惑。
请假设所有导入都已完成。
class ImageManip:
def __init__(self):
self.img_width, self.img_height = self.img.size
self.img_resize()
def img_resize(self):
while True:
clear()
try:
img_new_width = input(
'\n\nYour image\'s dimensions are:' +
'\nWidth: ' + self.img_width +
'\nHeight: ' + self.img_height +
'\n\nEnter the width: '
)
img_new_height = input(
'Enter the height: '
)
except TypeError:
print('Oh no! You didn\'t enter a number! Try again.')
time.sleep(2)
print('\n\n', end='')
continue
else:
self.img_final = self.img.thumbnail((img_new_width, img_new_height), Image.ANTIALIAS)
self.img_final.show()
break
System: Windows 10, 32bit
Version: Python 3.6
input()
需要用字符串调用。 self.img_height
和 self.img_width
是代码中的整数。
如果您对这些调用 str()
以将它们转换为字符串,它应该可以工作:
img_new_width = input(
'\n\nYour image\'s dimensions are:' +
'\nWidth: ' + str(self.img_width) +
'\nHeight: ' + str(self.img_height) +
'\n\nEnter the width: '
)
您可能还想使用 int()
将输入转换为整数:
img_new_width = int(input(
...
)
我做了一个 class,它使用了 PIL library
中 Image
模块中的一些 methods/functions(我不知道怎么称呼它们)。在此代码中,我要求用户输入要调整到的图像的新高度。因为我希望用户在出现错误时再次输入它,所以我将它放在 while
循环中。
我最初试图接受一个元组,然后将其解压缩到 new_height 和 new_width 变量中,但我认为这可能会使用户感到困惑。
请假设所有导入都已完成。
class ImageManip:
def __init__(self):
self.img_width, self.img_height = self.img.size
self.img_resize()
def img_resize(self):
while True:
clear()
try:
img_new_width = input(
'\n\nYour image\'s dimensions are:' +
'\nWidth: ' + self.img_width +
'\nHeight: ' + self.img_height +
'\n\nEnter the width: '
)
img_new_height = input(
'Enter the height: '
)
except TypeError:
print('Oh no! You didn\'t enter a number! Try again.')
time.sleep(2)
print('\n\n', end='')
continue
else:
self.img_final = self.img.thumbnail((img_new_width, img_new_height), Image.ANTIALIAS)
self.img_final.show()
break
System: Windows 10, 32bit
Version: Python 3.6
input()
需要用字符串调用。 self.img_height
和 self.img_width
是代码中的整数。
如果您对这些调用 str()
以将它们转换为字符串,它应该可以工作:
img_new_width = input(
'\n\nYour image\'s dimensions are:' +
'\nWidth: ' + str(self.img_width) +
'\nHeight: ' + str(self.img_height) +
'\n\nEnter the width: '
)
您可能还想使用 int()
将输入转换为整数:
img_new_width = int(input(
...
)