Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Learning python. Stuck.
New Posts  All Forums:Forum Nav:

Learning python. Stuck.

post #1 of 7
Thread Starter 
Not homework. Am learning from the "Learn Python the Hard Way" by Zed A. Shaw.

Am at exercise 16. Typing in the example works fine but I'm stuck on one of the study drills.
Code. (Click to show)
Code:
      1 from sys import argv
      2 
      3 script, filename = argv
      4 
      5 print "We're going to erase %r." % filename
      6 print "If you don't want that, hit CTRL-C (^C)."
      7 print "If you do want that, hit RETURN."
      8 
      9 raw_input("?")
     10 
     11 print "Opening the file..."
     12 target = open(filename, 'w')
     13 
     14 print "Truncating the file. Goodbye!"
     15 target.truncate()
     16 
     17 print "Now I'm going to ask you for three lines."
     18 
     19 line1 = raw_input("line 1: ")
     20 line2 = raw_input("line 2: ")
     21 line3 = raw_input("line 3: ")
     22 
     23 print "I'm going to write these to the file."
     24 
     25 #target.write(line1)
     26 #target.write("\n")
     27 #target.write(line2)
     28 #target.write("\n")
     29 #target.write(line3)
     30 #target.write("\n")
     31 
     32 print "And finally we close the file."
     33 target.close()
Quote:
Study Drill 3 for Exercise 16

3. There's too much repetition in this file. Use strings, formats, and escapes to print out line1, line2, and line3 with just one target.write() command instead of 6.

I am stumped here. 25-30 are commented out as I was trying to do the above but nothing I've tried has worked. Everything in the book so far doesn't seem to apply to a function call. Been trolling google for an hour or so, tried 40+ different ideas, nothing has worked as yet.

*EDIT* Probably not helping myself I'm trying to learn vi / vim at the same time here so I got my head going in all kinds of directions.
Edited by Tadaen Sylvermane - 4/13/14 at 8:49pm
 
Micromachine
(8 items)
 
 
CPUMotherboardGraphicsRAM
i7 4500u Inspiron 15 7000 Integrated 8gb 
Hard DriveOSMouse
120gb Samsung EVO SSD Debian Logitech m705 
CPUMotherboardRAMHard Drive
AMD 5350 Kabini AsRock AM1H-ITX 8gb Gskill Ripjaws (2x4) Samsung 120 Evo 
Hard DriveHard DriveOSCase
WD 1tb 2.5 WD 2tb USB 3.0 Ubuntu Server 16.04 Antec ISK 
  hide details  
Reply
 
Micromachine
(8 items)
 
 
CPUMotherboardGraphicsRAM
i7 4500u Inspiron 15 7000 Integrated 8gb 
Hard DriveOSMouse
120gb Samsung EVO SSD Debian Logitech m705 
CPUMotherboardRAMHard Drive
AMD 5350 Kabini AsRock AM1H-ITX 8gb Gskill Ripjaws (2x4) Samsung 120 Evo 
Hard DriveHard DriveOSCase
WD 1tb 2.5 WD 2tb USB 3.0 Ubuntu Server 16.04 Antec ISK 
  hide details  
Reply
post #2 of 7
Quote:
Originally Posted by Tadaen Sylvermane View Post

Not homework. Am learning from the "Learn Python the Hard Way" by Zed A. Shaw.

Am at exercise 16. Typing in the example works fine but I'm stuck on one of the study drills.
Code. (Click to show)
Code:
      1 from sys import argv
      2 
      3 script, filename = argv
      4 
      5 print "We're going to erase %r." % filename
      6 print "If you don't want that, hit CTRL-C (^C)."
      7 print "If you do want that, hit RETURN."
      8 
      9 raw_input("?")
     10 
     11 print "Opening the file..."
     12 target = open(filename, 'w')
     13 
     14 print "Truncating the file. Goodbye!"
     15 target.truncate()
     16 
     17 print "Now I'm going to ask you for three lines."
     18 
     19 line1 = raw_input("line 1: ")
     20 line2 = raw_input("line 2: ")
     21 line3 = raw_input("line 3: ")
     22 
     23 print "I'm going to write these to the file."
     24 
     25 #target.write(line1)
     26 #target.write("\n")
     27 #target.write(line2)
     28 #target.write("\n")
     29 #target.write(line3)
     30 #target.write("\n")
     31 
     32 print "And finally we close the file."
     33 target.close()
I am stumped here. 25-30 are commented out as I was trying to do the above but nothing I've tried has worked. Everything in the book so far doesn't seem to apply to a function call. Been trolling google for an hour or so, tried 40+ different ideas, nothing has worked as yet.

*EDIT* Probably not helping myself I'm trying to learn vi / vim at the same time here so I got my head going in all kinds of directions.

I haven't done this tutorial myself, but it's probably looking for something along the lines of:
Code:
target.write("{}\n{}\n{}\n".format(line1, line2, line3))
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
post #3 of 7
https://docs.python.org/2/library/string.html#format-examples
http://www.tutorialspoint.com/python/python_strings.htm
http://ebeab.com/2012/10/10/python-string-format/
Code:
target.write(line1+"\n"+line2+"\n"+line3+"\n")
or
target.write(("{0}\n{1}\n{2}\n").format(line1,line2,line3))
or
target.write("%s\n%s\n%s\n" % (line1,line2,line3))
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
post #4 of 7
Thread Starter 
Quote:
target.write("%s\n%s\n%s\n" % (line1,line2,line3))

This is what it's asking for. It fulfils all the requirements and is stuff I have learned already. My syntax was wrong though. I was doing like this.
Code:
target.write("%s\n%s\n%s\n") % (line1, line2, line3)

Thanks to both. Am reading the provided links right after I finish this post.drunken.gif

*EDIT* Having looked at the links above they seem to be pointing to python higher than 2.7. The book I am following is teaching me 2.7. Should I abandon the book and go straight for 3+ ?
Edited by Tadaen Sylvermane - 4/14/14 at 2:37pm
 
Micromachine
(8 items)
 
 
CPUMotherboardGraphicsRAM
i7 4500u Inspiron 15 7000 Integrated 8gb 
Hard DriveOSMouse
120gb Samsung EVO SSD Debian Logitech m705 
CPUMotherboardRAMHard Drive
AMD 5350 Kabini AsRock AM1H-ITX 8gb Gskill Ripjaws (2x4) Samsung 120 Evo 
Hard DriveHard DriveOSCase
WD 1tb 2.5 WD 2tb USB 3.0 Ubuntu Server 16.04 Antec ISK 
  hide details  
Reply
 
Micromachine
(8 items)
 
 
CPUMotherboardGraphicsRAM
i7 4500u Inspiron 15 7000 Integrated 8gb 
Hard DriveOSMouse
120gb Samsung EVO SSD Debian Logitech m705 
CPUMotherboardRAMHard Drive
AMD 5350 Kabini AsRock AM1H-ITX 8gb Gskill Ripjaws (2x4) Samsung 120 Evo 
Hard DriveHard DriveOSCase
WD 1tb 2.5 WD 2tb USB 3.0 Ubuntu Server 16.04 Antec ISK 
  hide details  
Reply
post #5 of 7
I am...
 
Project OP
(13 items)
 
 
CPUMotherboardGraphicsRAM
AMD Ryzen R7 1700x Asus x370 crosshair VI Hero ASUS ROG STRIX GeForce GTX 1080 TI G.SKILL TridentZ 16GB  
Hard DriveHard DriveOptical DriveCooling
Samsung 950 m.2 256GB  Crucial MX300 1TB SATA 2.5 HP DVD1070 Corsair H115i 
CoolingCoolingOSPower
Corsair ML140 Pro White (x7) Gnome Tech 560W/mk SSD Heatsink Windows 10 Home EVGA SuperNOVA 750 G2 80+ GOLD 
CaseOther
Be Quiet! Dark Base Pro 900  CableMod PSU Cabling 
CPUMotherboardGraphicsRAM
Intel Core i5-4690k  MSI Z97S SLI Krait Edition  Sapphire Nitro+ RX 570 Kingston HyperX 8GB(2x4) Black 
RAMHard DriveOptical DriveCooling
Kingston HyperX 8GB(2x4) White Samsung 950 Pro m.2 Lightscribe 24x DVDrw Corsair H100i v2 
OSMonitorPowerCase
Windows 10 LG 25UM57 2560 X 1080 60Hz IPS SilverStone Strider plus SST ST1000-P Corsair Carbide 400c White 
Other
Swiftech 8W-PWM 
  hide details  
Reply
 
Project OP
(13 items)
 
 
CPUMotherboardGraphicsRAM
AMD Ryzen R7 1700x Asus x370 crosshair VI Hero ASUS ROG STRIX GeForce GTX 1080 TI G.SKILL TridentZ 16GB  
Hard DriveHard DriveOptical DriveCooling
Samsung 950 m.2 256GB  Crucial MX300 1TB SATA 2.5 HP DVD1070 Corsair H115i 
CoolingCoolingOSPower
Corsair ML140 Pro White (x7) Gnome Tech 560W/mk SSD Heatsink Windows 10 Home EVGA SuperNOVA 750 G2 80+ GOLD 
CaseOther
Be Quiet! Dark Base Pro 900  CableMod PSU Cabling 
CPUMotherboardGraphicsRAM
Intel Core i5-4690k  MSI Z97S SLI Krait Edition  Sapphire Nitro+ RX 570 Kingston HyperX 8GB(2x4) Black 
RAMHard DriveOptical DriveCooling
Kingston HyperX 8GB(2x4) White Samsung 950 Pro m.2 Lightscribe 24x DVDrw Corsair H100i v2 
OSMonitorPowerCase
Windows 10 LG 25UM57 2560 X 1080 60Hz IPS SilverStone Strider plus SST ST1000-P Corsair Carbide 400c White 
Other
Swiftech 8W-PWM 
  hide details  
Reply
post #6 of 7
Quote:
Originally Posted by Tadaen Sylvermane View Post

This is what it's asking for. It fulfils all the requirements and is stuff I have learned already. My syntax was wrong though. I was doing like this.
Code:
target.write("%s\n%s\n%s\n") % (line1, line2, line3)

Thanks to both. Am reading the provided links right after I finish this post.drunken.gif

*EDIT* Having looked at the links above they seem to be pointing to python higher than 2.7. The book I am following is teaching me 2.7. Should I abandon the book and go straight for 3+ ?

That's a tricky question. 3.x is the future, but *everything* uses 2.7 at the moment (and that probably won't change any time soon with support for 2.7 being extended to 2020 and some companies needing to support it until almost 2030). If you learn 3.x (which I encourage), you still need to learn the differences between the two.

Consider this book to start with (the book is available online for free)
http://www.greenteapress.com/thinkpython/

It doesn't cover as the hard way does, but I think it's a better introduction. In addition, it teaches 2.7, but tells how 3.x is different in the footnotes.
Edited by hajile - 4/14/14 at 3:08pm
post #7 of 7
Thread Starter 
Am getting hold of that book. Thank you much.
 
Micromachine
(8 items)
 
 
CPUMotherboardGraphicsRAM
i7 4500u Inspiron 15 7000 Integrated 8gb 
Hard DriveOSMouse
120gb Samsung EVO SSD Debian Logitech m705 
CPUMotherboardRAMHard Drive
AMD 5350 Kabini AsRock AM1H-ITX 8gb Gskill Ripjaws (2x4) Samsung 120 Evo 
Hard DriveHard DriveOSCase
WD 1tb 2.5 WD 2tb USB 3.0 Ubuntu Server 16.04 Antec ISK 
  hide details  
Reply
 
Micromachine
(8 items)
 
 
CPUMotherboardGraphicsRAM
i7 4500u Inspiron 15 7000 Integrated 8gb 
Hard DriveOSMouse
120gb Samsung EVO SSD Debian Logitech m705 
CPUMotherboardRAMHard Drive
AMD 5350 Kabini AsRock AM1H-ITX 8gb Gskill Ripjaws (2x4) Samsung 120 Evo 
Hard DriveHard DriveOSCase
WD 1tb 2.5 WD 2tb USB 3.0 Ubuntu Server 16.04 Antec ISK 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Learning python. Stuck.