Note that there are some explanatory texts on larger screens.

plurals
  1. POTypeError: unsupported operand type(s) for -: 'str' and 'int' Writing a story?
    primarykey
    data
    text
    <p>I am trying to write a program (using Python 3.3.2) that asks for numbers then displays a story:</p> <p><a href="http://postimg.org/image/ayd5n7suv/" rel="nofollow">http://postimg.org/image/ayd5n7suv/</a></p> <p>But instead it outputs this: </p> <pre><code>Traceback (most recent call last): File "C:\Bladibla.py", line 40, in &lt;module&gt; remaining_sweets = (int(sweets) - int(swducks)*int(ducks - 1))*int(children) TypeError: unsupported operand type(s) for -: 'str' and 'int' </code></pre> <p>But I don't see any mistakes on that line. Why is this so?:</p> <pre><code>remaining_sweets = (int(sweets) - int(swducks)*int(ducks - 1))*int(children) </code></pre> <p>This is all of the code: </p> <pre><code>#Asks for number of children children = input("How many children are there?: ") if int(children) &gt; 100: print("That number is unimaginable! Please use a smaller number.") children = input("How many children are there?: ") if int(children) &lt; 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) &gt; 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) &lt; 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) &gt; 200 : print("That's too many ducks! Please use a smaller number!") ducks = input("How many ducks were there?: ") if int(ducks) &lt; 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) &gt; 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) &lt; 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.") </code></pre> <p>Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload