我试图在Hackerearth网站上解决一个练习问题陈述。以下是我的代码:
Length = int(raw_input())
NumberOfPhotoes = int(raw_input())
Width=list()
Height=list()
for i in range(NumberOfPhotoes):
w,h = map(int,raw_input().split())
Width.append(w)
Height.append(h)
#Height = int(raw_input())
if (Width[i] < Length) | (Height[i] < Length):
print "UPLOAD ANOTHER"
elif (Width[i] == Height[i] == Length):
print "ACCEPTED"
else:
print "CROP IT"
w = 0
h = 0
当我运行上面的代码时,在提供下面的第一组数据后显示错误消息:
180
3
640 480
CROP IT
追踪(最近的呼叫最后):
文件&#34; D:\ Project_Using_Eclipse \ HackerEarth_Practice \ Roy_and_profile_Picture.py&#34;,第6行,中
w,h = map(int,raw_input()。split())
ValueError:需要超过0个值才能解压缩
答案 0 :(得分:0)
获取输入,拆分,立即将其映射到int
,然后将其解压缩为两个变量可能看起来 neat 但是非常脆弱,因为任何非预期的输入(即任何非预期的输入)不是由空格分隔的两个整数)会破坏它。您的错误发生在其中一种情况下(我猜您在第三次提示后按Enter键)。
执行一些错误处理:
input_data = raw_input().split()
if len(input_data) < 2:
print("INVALID INPUT!") # to stay with the uppercase
continue # maybe you want to run this in a loop instead?
try:
w = int(input_data[0])
h = int(input_data[1])
except ValueError:
print("INVALID INPUT!") # to stay with the uppercase
continue # maybe you want to run this in a loop instead?
# now you're safe to use your `w` and `h` variables