[Tutor] Question
Timo
timomlists at gmail.com
Tue Aug 21 09:30:43 EDT 2018
Op 21-08-18 om 13:16 schreef Jacob Braig:
> I am just starting out coding and decided on python. I am confused with
> something I go shooting a lot so i wanted to make some stupid easy
> calculator for ammo and slowly build the program up when I understand
> python better but the code I have now keeps popping up an error and I don't
> understand where i went wrong a little help please.
> Here is the code like I said very simple stuff.
>
> """
> This is a simple program for ammo calculation.
> Created by Jacob
> """
> AMMO = " This is how much ammo remains for .40 pistol %s. "
>
> print "Program has started."
>
> ammopistol = raw_input("Enter total ammo before use. ")
> ammopused = raw_input("Enter total ammo used. ")
> ammopleft = ammopistol - ammopused
>
> print AMMO % (ammopistol, ammopused,) ammoleft
>
> I am having issues with " ammopleft = ammopistol - ammopused " any idea
> where I have gone wrong?
Always include the full traceback (error message).
The problem is that raw_input() returns a string, so you are doing
something like "10" - "2" instead of 10 - 2 for example. The error
message should have told you so:
TypeError: unsupported operand type(s) for -: 'str' and 'str'
The solution is to cast these inputs to integers, either directly during
input:
ammopistol = int(raw_input("Enter total ammo before use. "))
ammopused = int(raw_input("Enter total ammo used. "))
Or during calculation:
ammopleft = int(ammopistol) - int(ammopused)
Next issue will be your print statement. See the following website on
how to properly format strings: https://pyformat.info/
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list