Linux Lite Forums

Full Version: Stuck again, what's wrong?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to learn Python3 from the book Learn Python 3 the Hard Way and find it hard going especially when the code keeps on displaying an error. Take for instance, exercise 20.
Code:
# ex20: Functions and Files   
   from sys import argv   
   script, input_file = argv   
   def print_all(f):       print(f.read())   
   def rewind(f):       f.seek(0)   
def print_a_line(line_count, f):       print(line_count, f.readline())   
   current_file = open(input_file)   
   print("First let's print the whole file:\n")   
   print_all(current_file)   
   print("Now let's rewind, kind of like a tape.")   
   rewind(current_file)   
   print("Let's print three lines:")   
   current_line = 1   print_a_line(current_line, current_file)   
   current_line = current_line + 1   print_a_line(current_line, current_file)   
   current_line = current_line + 1   print_a_line(current_line, current_file)
I keep getting the following error
Code:
colin@my_new_LinuxLite4.0:~$ python3 ~/python_exp/ex20.pyTraceback (most recent call last):    File "/home/colin/python_exp/ex20.py", line 7, in <module>       script, input_file = argvValueError: not enough vales to unpack (expected 2, got 1)colin@my_new_LinuxLite4.0:~$
I've come across this error on a previous exercise but sadly misplaced my notes on how to remedy the error. Can someone help me please. 
Hello [member=6850]colin[/member] ,
it looks like you are running the file incorrectly as you are not specifying the command in terminal.
I do not have the Python3 book only the Python2, I had a try at this one though to help Smile .
The Python, notice the line changes and indentations.

[Image: colin1.png]

The test.txt file used (contents are his words not mine) from an exercise in the Python2 book, I just renamed it to use for this try.

[Image: colin2.png]

How to run it in terminal

[Image: colin3.png]

On highlight in terminal see #1, #2 and #3 above, these are the components of the command that you need.
For #2 you can either cd to the directory containing your Python files, or you can enter in your Path as previously. I just put it in Home directory as its temporary for this post.
#3 is what you have missed in the terminal command ?  or it is in another folder than the Python file is.
need to install gedit.

[Image: col1.png]


[Image: col2.png]

Python looks fun
[member=8093]Shanti[/member] great work, glad you liked Python 8)
Gedit up now  Smile  thanks for template help.


[Image: Screenshot_2018_09_27_17_42_28.png]
You are welcome [member=8093]Shanti[/member]  Smile It's looking good.
Thanks for the help once again bitsnpcs.
You are Welcome Colin  Smile