Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
str_format_2var
#1
"""
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??
Reply
#2
    print(args[0] % (args[1], args[2]))
Reply
#3
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
Reply
#4
This works thank you...
Reply
#5
(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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020