0

I wanted to try writing functions of a language to a different one. I opt for C to Python, and here is what I've done so far.

Code:

def printf(text, *args):
    print(text % args, end = '')

def scanf(text, *args):
    args = input(text % args)
    return args

name = None
scanf("What's your name? %s", name)
printf("Hello, %s.\n", name)

Result:

What's your name? NoneBoy
Hello, Boy.

I have 3 problems regarding this question:

  1. scanf doesn't actually print out the variable to be inputted.
  2. I have to implement a variable with no value, but unlike C, where you could just write int result;, you must write it as result = None and my function would print None as well.
  3. It never returned any value.

Are there any solutions I can use to fix these?

1 Answers1

0

You can use static variables for updating any value inside the function.

class Example:
    name = "None"

def printf(text, args):
    print(text % args, end = '')

def scanf(text, args):
    Example.name = input(text)

scanf("What's your name? ", name)
printf("Hello, %s.\n", Example.name)

There are other pre-build functions to use. This is the example of the user-defined functions. If you want to use a pre-defined function. Please let me know.

Vishal Sheth
  • 163
  • 1
  • 7