473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Improve my Functions?

Trying to set up a file for questions and answers. Doesn't seem to be working properly. Can anyone see where I can improve my functions? This is what I have so far:


questionFile = open('questions.txt')
numberQuestionDictionary = {}
for line in questionFile.readlines():
if not line:
continue
line = line.strip()
if len(line) == 0:
continue
numberAndQuestion = line.split('.')
number = numberAndQuestion[0]
question = numberAndQuestion[1].strip()
numberQuestionDictionary[number] = question
questionFile.close()

answerFile = open('answers.txt')
numberAnswerDictionary = {}
for line in answerFile.readlines():
if not line:
continue
line = line.strip()
if len(line) == 0:
continue
numberAndAnswer = line.split('.')
number = numberAndAnswer[0]
answer = numberAndAnswer[1].strip()
numberAnswerDictionary[number] = answer
answerFile.close()

for number in numberQuestionDictionary.keys():
print 'Q: ',
print numberQuestionDictionary[number]
print 'A: ',
print numberAnswerDictionary[number]
print
Nov 22 '06 #1
1 1316
bartonc
6,596 Expert 4TB
What you have so far looks OK. The files that you are reading must be in the same directory as this module. You must read the gray writing on the background of the post window or the "Posting Guidelines" on the right or at the top of this forum to learn how to use CODE TAGS so that your post looks like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. # Read number and question from a file into a dict
  3. questionFile = open('questions.txt')
  4. numberQuestionDictionary = {}
  5. for line in questionFile.readlines():
  6.     if not line:
  7.         continue
  8.     line = line.strip()
  9.     if len(line) == 0:
  10.         continue
  11.     numberAndQuestion = line.split('.')
  12.     number = numberAndQuestion[0]
  13.     question = numberAndQuestion[1].strip()
  14.     numberQuestionDictionary[number] = question
  15. questionFile.close()
  16. # Read number and answer from a file into a dict
  17. answerFile = open('answers.txt')
  18. numberAnswerDictionary = {}
  19. for line in answerFile.readlines():
  20.     if not line:
  21.         continue
  22.     line = line.strip()
  23.     if len(line) == 0:
  24.         continue
  25.     numberAndAnswer = line.split('.')
  26.     number = numberAndAnswer[0]
  27.     answer = numberAndAnswer[1].strip()
  28.     numberAnswerDictionary[number] = answer
  29. answerFile.close()
  30.  
  31. for number in numberQuestionDictionary.keys():
  32.     print 'Q: ',
  33.     print numberQuestionDictionary[number]
  34.     print 'A: ',
  35.     print numberAnswerDictionary[number]
  36.     print
  37.  
We'll also need a copy of the text files that you are trying to read. Thanks for posting,
Barton
Nov 22 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: lkrubner | last post by:
Last year I asked a bunch of questions about character encoding on this newsgroup. All the answers came down to using ord() in creative ways to try to make guesses about multi-byte characters. I...
10
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x";...
17
by: Sean Kenwrick | last post by:
I am writing a byte-code interpreter/emulator for a language that exclusively uses strings for variables (i.e all variables are pushed onto the stack as strings). Therefore for arithmetic...
0
by: S Kemplay | last post by:
Hi all, I am after any pointers you may have to improve these two functions. They both work but are part of an assignment - I am looking for feedback. The first one reads records from a file...
9
by: Peng Jian | last post by:
I have a function that is called very very often. Can I improve its efficiency by declaring its local variables to be static?
41
by: Petr Jakes | last post by:
Hello, I am trying to study/understand OOP principles using Python. I have found following code http://tinyurl.com/a4zkn about FSM (finite state machine) on this list, which looks quite useful for...
5
by: Cenk Genisol | last post by:
Hi NG, I am relatively new to Web developement and have some questions to you. I've got a Masterpage where some graphics are embedded by CSS. They format the borders of tables, which store...
75
by: At_sea_with_C | last post by:
Hello all, I have written an ascending sort routine for floats. This seems to do the job, but for elements over 10,000, it gets awfully slow. A lot of useless comparisions with previously sorted...
11
by: Peted | last post by:
Im using c# 2005 express edition Ive pretty much finished an winforms application and i need to significantly improve the visual appeal of the interface. Im totaly stuck on this and cant seem...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.