数字を要求してからストーリーを表示するプログラム(Python 3.3.2を使用)を作成しようとしています:
http://postimg.org/image/ayd5n7suv/
しかし、代わりにこれを出力します:
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'
しかし、その行に間違いはありません。なぜそうなのか?:
remaining_sweets = (int(sweets) - int(swducks)*int(ducks - 1))*int(children)
これはすべてのコードです:
#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.")
前もって感謝します