Ex26 - arguing with arguments

Hi All,

I’ve run into a bit of a problem with Ex26 (enjoying it a lot, by the way).
I have worked through the code and corrected as many errors as I could see, then started running it in Powershell. It runs up to the point between lines 10-23, and this is where my problem lies. When the code reaches this point, I get an error relating to line 11:

Traceback (most recent call last):
File “ex26.py”, line 11, in
script, filename = argv
ValueError: not enough values to unpack (expected 2, got 1)

So, I understand that the program is looking for two values, but only getting one (filename?). Am I right in thinking that there should be another line defining script?

Any help would be greatly appreciated. Code is appended below. Thanks in advance,


print("How old are you?", end=' ')
age = input()
print("How tall are you?", end=' ')
height = input() #missing height input line - fixed
print("How much do you weigh?", end=' ') #think this is missing a ")" - fixed
weight = input()

print(f"So, you're {age} old, {height} tall and {weight} heavy.")

from sys import argv #added this line
script, filename = argv

txt = open(filename) #misspelled filename here - fixed

print("Here's your file {filename}:")
print(txt.read()) #misspelled txt - fixed

print("Type the filename again:")
file_again = input("> ")

txt_again = open(file_again)

print(txt_again_read()) #I think there is something wrong with this, but not sure.

print("Let's practice everything.")
print('You\'d need to know \'bout escapes with \\ that do: \n newlines and \t tabs.') #don't spread this over two lines

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.

print("--------------") #missing " before ) - fixed
print("--------------") #missing " after ( - fixed

five = 10 - 2 + 3 - 6 #missing figure here, or additional arithmetic operator - fixed
print(f"This should be five: {five}") #missing close bracket - fixed

def secret_formula(started): #missing : after (started) - fixed
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100 #missing divided symbol - fixed
    return jelly_beans, jars, crates

start_point = 10000
beans, jars, crates = secret_formula(start_point) #missing crates - fixed

# remember that this is another way to format a string
print("With a starting point of: {}".format(start_point))
# it's just like with an f"" string
print(f"We'd have {beans} beans, {jars} jars, and {crates} crates.")

start_point = start_point / 10

print("We can also do that this way:")
formula = secret_formula(start_point) #missing underscore between start and point in brackets - fixed
# this is an easy way to apply a list to a format string
print("We'd have {} beans, {} jars, and {} crates.".format(*formula))

people = 20
cats = 30 #variable cats misspelled - fixed
dogs = 15

if people < cats:
    print ("Too many cats! The world is doomed!") #missing brackets around phrase to be printed - fixed.

if people < cats:
    print("Not many cats! The world is saved!")

if people < dogs:
    print("The world is drooled on!")

if people > dogs: #missing : after dogs - fixed
    print("The world is dry!")

dogs += 5

if people >= dogs:
    print("People are greater than or equal to dogs.")

if people <= dogs: #missing : after dogs - fixed
    print("People are less than or equal to dogs.") #missing " after dogs.

if people == dogs: # = should be == - fixed
    print("People are dogs.")

Hi @Hammer_53

I’m just a few lessons ahead of you, but I think I know what’s wrong.

You need to move

from sys import argv 
filename = argv

to the start of your code, as you have to tell the name of your filename when you start the script

Then I found you are missing a ‘.’

print(txt_again_read()) #I think there is something wrong with this, but not sure.


print(txt_again.read()) #I think there is something wrong with this, but not sure.

Then it seems to work in my end.

When you are running this in powershell you must write the filename also.
For example:
ex26.py your_filename(Write the name of the file)

Try this I hope it works.

Awesome! Thanks very much!

Of course! With the arguments, you have to run it on a file. I’d forgotten that. Thanks!

Looks like you solved it but really quick:

script, filename = argv

The argv is a list which is just a…list…that contains two strings in it:

  1. The name of the python script: “ex26.py”
  2. The name of the file you typed on the command line after ex26.py.

You can see what’s inside argv with:


What that line does is “explode” the argv list out into the two variables script and filename. It’s the same as doing this:

script = argv[0]
filename = argv[1]
1 Like

That makes a lot of sense, thanks for the breakdown.
I created a simple text file, and used its name as my filename argument when running ex26.py, but powershell is saying “no such file or directory” for the file that I called. I’ve checked and the file is clearly there in the same directory as the exercise. I’ll go back and look at the code immediately after, relating to what it was going to do with the file.

Thanks again for the help, folks.

If you get that error then the file is not there. Do ls (ell ess) to see what’s there. This usually happens because people don’t save the file in their text editor.

Hi Zed,
Thanks, I had checked this already, using ls. I created a simple text file, called “sparps”, and it’s visible in the same directory as my exercise files (bottom file). I’ll paste a screenshot below. Tried using “sparps” and “sparps.txt” as my filename argument, but neither worked.



You should probably post the error too. Also you don’t need to post an image, just do this:

# paste your PS error output ehre

That’s way easier to read. Chances are you are getting this error but it has nothing to do with the actual file you think it’s working on. You should print out the variable your passing to open on the line that’s listed. For example:

txt_again = open(file_again)

Do this:

print(">>> file again=", file_again)
txt_again = open(file_again)

If the file is there and you get an error trying to open it then probably file_again is wrong, so print it out.

I would rewrite it like this;

age = input("How old are you? ")
height = input("How tall are you? ")
weight = input("How much do you weigh? ")

print(f"So, you're {age} old, {height} tall and {weight} heavy.")

**from** sys **import** argv
script, filename = argv  # in powershell type python ex26.py sample.txt or whatever your txt file is called

print(f"Here's your file {filename}:") # you forgot the f

print(txt_again.read()) # yes you wrote _ underscore instead of .read()

I hope this helps

1 Like

Thanks Catherine. Appreciate you taking the time to spot where I went wrong!