Python Forum

Full Version: str_format_2var
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
"""
Create a function called str_format_2var, taking three arguments:
  - A string, containing two formatting markers supported by Python,
  like %s, %d, etc.
  - Value to be substituted for the first formatting marker
  - Value to be substituted for the second formatting marker

The function must:
  1. Return a single string, containing where the formatting markers are
  replaced with the second and third function arguments
  2. When inspected, would return the following help string:
    Replace two formatting markers with the variables that were passed in

For example:
  str_format_2var('%d %s bottles', 10, 'green') -> '10 green bottles'
  str_format_2var('%s is %.2f', 'Pi', 3.14) -> 'Pi is 3.14'
"""

def str_format_2var(*args):
    print (args[0], % { "d" : args[1], 's': args[2]})

x = '%d %s bottles'
y = 10
z = 'green'

str_format_2var(x,y,z)
What am i doing wrong in this??
    print(args[0] % (args[1], args[2]))
There are a few other issues in addition to what heiner55 already wrote:
  • You're supposed to return the string, not print it
  • Your function doesn't have a docstring
  • You shouldn't be using *args if you know the number of arguments you will take; take exactly three instead
This works thank you...
(Nov-29-2016, 05:28 PM)roadrage Wrote: [ -> ]This works thank you...
It's an improvement, but as stranac pointed out it's not the solution. If I were your teacher, you'd get a 0 for what it seems you have here. If anything isn't clear, please make sure to ask us instead of ignoring it.