1

Using Visual Studio & http://pythontutor.com/visualize I am unable to get the result for filesize function because of the following Error: TypeError: file_size() takes 1 positional argument but 3 were given. Code below.

#this function stores information about a file, its name, its type and its size in bytes. def file_size(file_info): name, file_type, size = file_info return("{:.2f}".format(size/1024)) print(file_size('Class Assignment','docx', 17875)) #Should print 17.46 print(file_size('Notes','txt', 496)) #Should print 0.48 print(file_size('Program','py', 1239)) #Should print 1.21

I though unpacking (file_info) within the function will override the 1 positional argument but 3 were given error. What am I doing wrong?

2
  • 1
    file info is not a tuple, you've passed 3 strings. Commented Apr 25, 2020 at 2:35
  • 1
    You need just one extra character: def file_size(*file_info): if you want to call the function this way. Commented Apr 25, 2020 at 2:35

5 Answers 5

1

Currently you are passing three distinct arguments ... pass it instead as one argument which is a tuple:

print(file_size( ('Class Assignment','docx', 17875) )) 

Alternatively, you could alter your function declaration to capture distinct arguments into a tuple when called:

def file_size(*file_info): ... 

Then calling it as you are is valid.

Sign up to request clarification or add additional context in comments.

Comments

1

Put the values into a tuple like this:

print(file_size(('Class Assignment','docx', 17875))) print(file_size(('Notes','txt', 496))) print(file_size(('Program','py', 1239))) 

This way you are passing one variable

Comments

0

Please try this:

def file_size(file_info): name, type, size= file_info return("{:.2f}".format(size / 1024)) 

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
0

Please try this.

def file_size(file_info): (name,file_type,size) = file_info return("{:.2f}".format( size/ 1024)) 

You need to pass file_info in a tuple in python.

Comments

0

If you want to keep the function with one parameter, try assigning the tuple to a variable and pass it in:

file_size(*varname) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.