How to use variabled function with formatted string?

21 Views Asked by At

I need to use my function as a variable but when I tried to use it with formatted string it print the variable ID, but when I use it like this -->

dot = efdot

dot()

It prints function. Here is my code I summarized it.

code:

import time

    def efdot():
        bar = "⦾⦿"
        index = 0
        while True:
            print(bar[index % len(bar)], end="\r")
            index += 1
            time.sleep(0.5)
    
    
    dot = efdot
    var = input(f"{dot}Hello World{dot}")

output:

<function efdot at 0x0000016EDCEF3E20>Hello World<function efdot at 0x0000016EDCEF3E20>
1

There are 1 best solutions below

1
exniagara On

You need to call your function with the parentheses: dot()

import time

def efdot():
    bar = "⦾⦿"
    return bar

dot = efdot
var = input(f"{dot()}Hello World{dot()}")


⦾⦿Hello World⦾⦿

But because your function never ends with that infinite loop, it won't print the rest of the string.

Summary:

  • You need to call the function in the string
  • It won't work as you expected because of the infinite loop