[Tutor] Question

Alan Gauld alan.gauld at yahoo.co.uk
Tue Aug 21 13:35:58 EDT 2018


On 21/08/18 12:16, Jacob Braig wrote:
> I am just starting out coding and decided on python. 

It looks like you are using Python v2 (maybe v2.7?) but
the latest version is 3.7 and for beginners we normally
recommend adopting v3. It doesn't change anything much
in this case but it saves you relearning too much later
if you decide to switch.

> python better but the code I have now keeps popping up an error 

Always include the full error text so we don't
need to guess. It contains a lot of useful information.

> AMMO = " This is how much ammo remains for .40 pistol %s. "
> 
> print "Program has started."
> 
> ammopistol = raw_input("Enter total ammo before use. ")

raw_input() does what it says, it reads the raw characters
typed at the keyboard. It does not try to guess what they
are - in this case numbers - it just stores them as a
character string.

You need to convert them to the type of data you need.

ammopistol = int( raw_input("Enter total ammo before use. ") )

> ammopused = raw_input("Enter total ammo used. ")

same here

> ammopleft = ammopistol - ammopused

You cannot subtract character strings so you got an
error, but once you convert the data it should work.

> print AMMO % (ammopistol, ammopused,) ammoleft

This won't work however.
Your AMMO string has one % placeholder in it so it
expects exactly 1 value on the right of the % sign.
You are providing a tuple of two values. Then you add
a third value that's not in the tuple and is invalid
syntax in Python. Python won't know what to do and
you will get another error.

You really want something like

print AMMO % ammoleft

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list