Development > Coding

Stuck again, what's wrong?

(1/2) > >>

colin:
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)

--- End code ---
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:~$

--- End code ---
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. 

bitsnpcs:
Hello @colin ,
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 :) .
The Python, notice the line changes and indentations.



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.



How to run it in terminal



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.

Shanti:
need to install gedit.






Python looks fun

bitsnpcs:
@Shanti great work, glad you liked Python 8)

Shanti:
Gedit up now  :)  thanks for template help.


Navigation

[0] Message Index

[#] Next page

Go to full version