简体   繁体   中英

Trying to format the value of a function

I'm currently going through some of the study drills in a book I'm reading and the author wants us to try to run a function ten different ways. I'm currently at seven and am struggling with the eighth.

Mind you, I'm not entirely literate in terms of programming, but I'm trying to improve. Here's my problem.

function(arg1, arg2):
    print(f"I have {arg1} letters.")
    print(f"And I have {arg2} friends.")
    print("Wow, I need more friends...\n")

function('{} {}'.format(1, 2))

When I try to run this script it's telling me I'm missing a provisional argument (arg2); however, I thought I was including the proper syntax to successfully unpack these two arguments. Where am I going wrong?

I also tried calling two variables.

function(arg1, arg2):
    print(f"I have {arg1} letters.")
    print(f"And I have {arg2} friends.")
    print("Wow, I need more friends...\n")

argument1 = 1
argument2 = 2 

function('{} {}'.format(argument1, argument2)) 

Thanks!

You are passing a single string to function ; it doesn't matter that that string has 2 values within it. Nor does it matter if you use variables or constants (or even a mix of the two) to build that string; the same single string is passed to function either way.

I suppose you could split that string, but that seems a very roundabout way to pass 2 numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM