try:
f = int(factor)
if (factor == 0):
print("No factors present to derive");
elif (factor != int):
print ("Numbers only please!");
elif (factor >> 4):
print("Four maximum factors!");
else:
f = int(factor);
if f == 1:
coefficientone = raw_input("What is the coefficient on the first term?")
try:
coef1 = int(coefficientone)
if (coef1 == 0):
print "No coefficient present, please re-enter polynomial"
elif (coef1 != int)
print "Numbers only please!"
else:
coef1 = int(coefficientone)
这会在if f == 1:行返回语法错误。请帮忙!从我在这个网站上看到的和大多数其他网站看起来是正确的。我也很感激代码的任何其他部分的任何帮助,因为这是我第一次使用python。提前致谢。
答案 0 :(得分:2)
如果您要添加try
块,则必须使用匹配的except
来处理您正在捕获的异常。
如果你不知道为什么要添加try
块,最好只是删除它,以免掩盖潜在的错误。最好只有except
个特定的错误类型。
这是固定代码。这些变化都附有评论。
try:
f = int(factor)
if (factor == 0):
print("No factors present to derive");
elif (factor != int):
print ("Numbers only please!");
elif (factor >> 4):
print("Four maximum factors!");
else:
f = int(factor);
except Exception as e: # Add this
print 'ERROR: {0}'.format(e) #
if f == 1:
coefficientone = raw_input("What is the coefficient on the first term?")
try: # Un-indent this
coef1 = int(coefficientone)
if (coef1 == 0):
print "No coefficient present, please re-enter polynomial"
elif (coef1 != int)
print "Numbers only please!"
else:
coef1 = int(coefficientone)
except Exception as e: # Add this
print 'ERROR: {0}'.format(e) #
最后,您似乎计划向用户询问他指定的系数。为此,您可能需要考虑使用for
循环来处理输入,并使用list
来存储值。我将把实施细节留给您。那里有很多资源。
答案 1 :(得分:0)
#an error that I am seeing is that factor != int should be:
elif type(factor) != int:
#and adding to the try I think that you will also need a except ValueError: