01110100
01110100

Reputation: 833

Why isn't my %s format specifier not catching in this snippet?

mconfigQuantity = int(raw_input('Enter the number of (m)achine-configurations that you will need: '))
numberOfmconfig = 1


for mconfigs in range(1,mconfigQuantity+1):
    # %s conditional for mcidentifier
    STRINGnumberOfmconfig = str(numberOfmconfig)
    if STRINGnumberOfmconfig[-1] == '1':
        suffix = 'st'
    elif STRINGnumberOfmconfig[-1] == '2':
        suffix = 'nd'
    elif STRINGnumberOfmconfig[-1] == '3':
        suffix = 'rd'
    else: suffix = 'th'
    finalConfigName = STRINGnumberOfmconfig + suffix
    mcidentifier = raw_input('Enter the letter of your %s (m)achine-configuration: ') % (finalConfigName)
    numberOfmconfig = numberOfmconfig + 1






print mconfig()

I have written and rewritten this snippet a few times now, and it makes no sense to my as to why it is printing '%s' rather than replacing it with the data that I have specified for it. I can't find anything that indicates why it is not working.

Upvotes: 0

Views: 75

Answers (1)

Lev Levitsky
Lev Levitsky

Reputation: 65821

It should be

mcidentifier = raw_input(
    'Enter the letter of your %s (m)achine-configuration: ' % finalConfigName)

You closed the parenthesis before the % operator so it was applied to the resulting string, i.e. the one returned by raw_input.

Also note that in new code the new string formatting syntax is recommended:

mcidentifier = raw_input(
 'Enter the letter of your {} (m)achine-configuration: '.format(finalConfigName))

Upvotes: 5

Related Questions