Python TypeError:';的操作数类型不受支持;str';和';int';写故事?

Python TypeError:';的操作数类型不受支持;str';和';int';写故事?,python,python-3.x,Python,Python 3.x,我正在尝试编写一个程序(使用Python 3.3.2),该程序要求输入数字,然后显示一个故事: 但它输出的却是: Traceback (most recent call last): File "C:\Bladibla.py", line 40, in <module> remaining_sweets = (int(sweets) - int(swducks)*int(ducks - 1))*int(children) TypeError: unsupported o

我正在尝试编写一个程序(使用Python 3.3.2),该程序要求输入数字,然后显示一个故事:

但它输出的却是:

Traceback (most recent call last):
  File "C:\Bladibla.py", line 40, in <module>
    remaining_sweets = (int(sweets) - int(swducks)*int(ducks - 1))*int(children)
TypeError: unsupported operand type(s) for -: 'str' and 'int'
以下是所有代码:

#Asks for number of children
children = input("How many children are there?: ")
if int(children) > 100:
    print("That number is unimaginable! Please use a smaller number.")
    children = input("How many children are there?: ")
if int(children) < 0:
    print("That number is unimaginable! Please use a larger number.")
    children = input("How many children are there?: ")

#Asks for number of sweets
sweets = input("How many sweets do they have each?: ")
if int(sweets) > 100:
    print("If they ate that number of sweets, they would die! Please use a smaller number.")
    sweets = input("How many sweets do they have each?: ")
if int(sweets) < 0:
    print("How can they have a minus number of sweets? They can't vomit the sweets!!!! Please use a larger number.")
    sweets = input("How many sweets do they have each?: ")

#Asks for number of ducks
ducks = input("How many ducks were there?: ")
if int(ducks) > 200 :
    print("That's too many ducks! Please use a smaller number!")
    ducks = input("How many ducks were there?: ")
if int(ducks) < 0:
    print("How can there be a minus number of ducks?!! Please use a larger number")
    ducks = input("How many ducks were there?: ")

#Asks for number of sweets gave to each duck
swducks = input("How many sweets did each child give to each duck?: ")
if int(swducks) > 200 :
    print("That's too many sweets given to the ducks! Please use a smaller number!")
    swducks = input("How many sweets did each child give to each duck?: ")
if int(swducks) < 0:
    print("How can there be a minus number of sweets given to the ducks?!! Please use a larger number")
    swducks = input("How many sweets did each child give to each duck?: ")

#Outputs the 'thrilling' story
print("Please wait...")
print("There were " + children + " children each with a bag containg " + sweets + " sweets. They walked past " + ducks + " ducks. Each child gave " + swducks + " sweets to each of the ducks and ate one themselves. They decided to put the rest into a pile.")
remaining_sweets = (int(sweets) - int(swducks)*int(ducks - 1))*int(children)
print("They counted the pile and found it contained " + str(remaining_sweets) + " sweets.")
#询问孩子的数量
children=输入(“有多少个孩子?:”)
如果int(儿童)>100:
打印(“那个数字太不可思议了!请用一个更小的数字。”)
children=输入(“有多少个孩子?:”)
如果int(子项)<0:
打印(“这个数字太不可思议了!请用大一点的数字。”)
children=输入(“有多少个孩子?:”)
#询问糖果的数量
糖果=输入(“他们每个有多少糖果?:”)
如果int(糖果)>100:
打印(“如果他们吃了那么多的糖果,他们会死的!请使用较小的数字。”)
糖果=输入(“他们每个有多少糖果?:”)
如果int(sweets)<0:
打印(“他们怎么能有负数的糖果?他们不能吐糖果!!!!请使用更大的数字。”)
糖果=输入(“他们每个有多少糖果?:”)
#询问鸭子的数量
鸭子=输入(“有多少只鸭子?:”)
如果int(鸭子)>200:
打印(“鸭子太多了!请用小一点的数字!”)
鸭子=输入(“有多少只鸭子?:”)
如果int(ducks)<0:
打印(“怎么会有负数的鸭子?!!请使用更大的数字”)
鸭子=输入(“有多少只鸭子?:”)
#询问给每只鸭子的糖果数量
swducks=input(“每个孩子给了每只鸭子多少糖果?:”)
如果int(SWD)>200:
打印(“给鸭子的糖果太多了!请用小一点的数字!”)
swducks=input(“每个孩子给了每只鸭子多少糖果?:”)
如果int(swducks)<0:
打印(“给鸭子的糖果怎么可能是负数?!请使用更大的数字”)
swducks=input(“每个孩子给了每只鸭子多少糖果?:”)
#输出“惊险”故事
打印(“请稍候…”)
打印(“有“+children+”个孩子,每个孩子都有一个袋子,里面装着“+sweets+”糖果。他们走过“+ducks+”只鸭子。每个孩子给每只鸭子“+swducks+”糖果,自己吃一只。他们决定把剩下的放进一堆。”)
剩余的甜食=(int(甜食)-int(野鸭)*int(野鸭-1))*int(儿童)
打印(“他们数了数这堆糖果,发现里面有”+str(剩余的糖果)+“糖果”。)

提前感谢

ducks
是一个字符串,因此
ducks-1
失败

在减去1之前,必须先将其转换为
int

remaining_sweets = (int(sweets) - int(swducks)*int(ducks) - 1)*int(children)

这么多代码中的一个小错误!我得更加小心!谢谢,我希望能把这个作为我接受的答案。
(int(swsweets)-int(swducks)*(int(ducks)-1))*int(children)
?不,Martijn Pieters回答了这个问题。我需要int(ducks)-1而不是int(ducks-1)
remaining_sweets = (int(sweets) - int(swducks)*int(ducks) - 1)*int(children)