The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Newbie here, wondering what's up with this code..
Discuss Newbie here, wondering what's up with this code.. in the Python Programming forum on Dev Shed. Newbie here, wondering what's up with this code.. Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 1st, 2012, 12:46 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 1 h 54 m 33 sec
Reputation Power: 0
|
|
|
Newbie here, wondering what's up with this code..
I've been tinkering with Python as my first computer programming language (unless you count Batch and Bash as fully-fledged languages). I think I've done pretty well overall, keeping it neat and easy to read, but the highlighted piece is what I'm having trouble with. I found the code around the web (googled "python append text") and tried to wrestle with it.
Thanks in advance!
Code:
#Python Application
#login_user.py
#Pathname Finder
import sys, os
pathname = os.path.abspath(pathname)
class start_app:
def open_menu(self):
print "Welcome to the [redacted]!\n"
print "---------------\n"
print "Please log in!\n"
time.sleep(5)
username = raw_input("> ")
f = open(user_info.txt", "r")
searchlines = f.readlines()
f.close()
for i, line in enumerate(searchlines):
if username in line: print line
else f = open("user_info.txt", "a") as myfile:
myfile.write(username)
searchfile.close()
start_object = start_app()
start_object.open_menu(self)
print "Thanks for looking at my script!"
|

November 1st, 2012, 02:10 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Code:
f = open(user_info.txt", "r")
Here you have a missing quote: "user_info.txt" lacks the opening one.
Code:
else f = open("user_info.txt", "a") as myfile:
This is totally wrong. “else” should always have a trailing colon. Also, there shouldn’t be anything following it. However, “f = open(...) as myfile:” is not correct in itself. Maybe “else” should be “with”?
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|

November 1st, 2012, 01:32 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 1 h 54 m 33 sec
Reputation Power: 0
|
|
|
Thanks!
Quote: | Originally Posted by SuperOscar
Code:
f = open(user_info.txt", "r")
Here you have a missing quote: "user_info.txt" lacks the opening one.
Code:
else f = open("user_info.txt", "a") as myfile:
This is totally wrong. “else” should always have a trailing colon. Also, there shouldn’t be anything following it. However, “f = open(...) as myfile:” is not correct in itself. Maybe “else” should be “with”? |
Thanks for the feedback! Again, I have very little experience; everything highlighted is code I had just googled to help fit in.
I'm not sure where I can find a definitive guide on commands etc., though I would really love to learn more.
Thank you!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|