944,113 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 6751
  • Python RSS
May 13th, 2007
0

Starting a project in python (Game and quiz)

Expand Post »
Please help me, I want to make a quiz in python (And a game, if I get the time), can someone help me?

I saw the topic projects for beginners, but it was a .dat file (which I opened using notepad 2), I don't know how to use a dat file with python, I am used to .py files.

And .html files.

Please help,

Panarchy
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Panarchy is offline Offline
29 posts
since May 2007
May 13th, 2007
0

Re: Starting a project in python (Game and quiz)

Welcome to DaniWeb and the interesting world of Python coding!

The Quiz.dat file is just a text file containing 6 lines per question. The first line is the correct answer code for the four possible answers (A, B, C, D), followed by the question and the four possible answers. For example ...
Python Syntax (Toggle Plain Text)
  1. A
  2. Which sport uses the term LOVE ?
  3. Tennis
  4. Golf
  5. Football
  6. Swimming
  7. B
  8. What is the German word for WATER ?
  9. Wodar
  10. Wasser
  11. Werkip
  12. Waski
  13. C
  14. What has eight bits ?
  15. Word
  16. ASCII
  17. Byte
  18. RAM
  19. ...
  20. ...
To work with it in Python, it is easiest to form a tuple/array of each question in a set order and put them in a list. I picked the order (question, answer1, answer2, answer3, answer4, answer_code). The reason I used a list of tuples is that you can shuffle them easily. You read the data file into your program as a list of text lines and then convert these to a list of the question tuples ...
python Syntax (Toggle Plain Text)
  1. [('Which sport uses the term LOVE ?', 'Tennis', 'Golf', 'Football', 'Swimming', 'A'),
  2. ('What is the German word for WATER ?', 'Wodar', 'Wasser', 'Werkip', 'Waski', 'B'),
  3. ('What has eight bits ?', 'Word', 'ASCII', 'Byte', 'RAM', 'C'),
  4. ...]
Here is a short Python program to handle the formation of the list of tuples ...
python Syntax (Toggle Plain Text)
  1. # read the mutliple choice quiz data file
  2. # reorganize into a list of tuples, each tuple has this order
  3. # (question, answer1, answer2, answer3, answer4, answer_code)
  4.  
  5. # read the appended text file as a list of lines
  6. fin = open("Quiz.dat", "r")
  7. lineList = fin.readlines()
  8. fin.close()
  9.  
  10. quiz_list = []
  11. for k, line in enumerate(lineList):
  12. tuple_complete = False
  13. line1 = line.strip()
  14. # build a tuple for each question
  15. if k % 6 == 0:
  16. answer_code = line1
  17. if answer_code == 'end':
  18. break
  19. if k % 6 == 1:
  20. question = line1
  21. if k % 6 == 2:
  22. answer1 = line1
  23. if k % 6 == 3:
  24. answer2 = line1
  25. if k % 6 == 4:
  26. answer3 = line1
  27. if k % 6 == 5:
  28. answer4 = line1
  29. tuple_complete = True
  30. if tuple_complete:
  31. tuple1 = (question, answer1, answer2, answer3, answer4, answer_code)
  32. print tuple1 # test
  33. quiz_list.append(tuple1)
  34.  
  35. print "-"*70
  36. #print quiz_list # for testing
  37. print "-"*70
  38.  
  39. # shuffle the list and display the first 10 question tuples
  40. import random
  41. random.shuffle(quiz_list)
  42. for k, quest in enumerate(quiz_list):
  43. if k < 10:
  44. print quest
Now its up to you to display the question, followed by the four possible answers titled A, B, C, D and ask the player to give the title letter of the correct answer. After the user gives the input, then you compare the letter with the letter of the correct answer.
Last edited by vegaseat; May 13th, 2007 at 3:44 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
May 13th, 2007
0

Re: Starting a project in python (Game and quiz)

Thanks for that.

What should I write into the html?
Reputation Points: 10
Solved Threads: 0
Light Poster
Panarchy is offline Offline
29 posts
since May 2007
May 14th, 2007
0

Re: Starting a project in python (Game and quiz)

I am a bit of a noob. I have only just started learning python. Please help I don't understand this.
Reputation Points: 10
Solved Threads: 0
Light Poster
Panarchy is offline Offline
29 posts
since May 2007
May 14th, 2007
0

Re: Starting a project in python (Game and quiz)

Click to Expand / Collapse  Quote originally posted by Panarchy ...
I am a bit of a noob. I have only just started learning python. Please help I don't understand this.
If you are that unfamiliar with Python, I would recommend to work yourself through a simple tutorial first, and then a few simple projects. The Quiz program is more of medium difficulty. You have to know about lists, tuples, random functions, file handling and so on.

Here is a good recommendation of tutorial sources posted a few days ago:
http://www.daniweb.com/techtalkforums/post363044-4.html

What dont you understand?

Editors note: This thread refers to http://www.daniweb.com/techtalkforum...161212-13.html
Last edited by vegaseat; May 14th, 2007 at 5:46 pm. Reason: note
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 8th, 2007
0

Re: Starting a project in python (Game and quiz)

Thanks anyways. I will just learn C, then C++, then Java.
Reputation Points: 10
Solved Threads: 0
Light Poster
Panarchy is offline Offline
29 posts
since May 2007
Jun 8th, 2007
0

Re: Starting a project in python (Game and quiz)

Click to Expand / Collapse  Quote originally posted by Panarchy ...
Thanks anyways. I will just learn C, then C++, then Java.
You're finding difficulties with Python and "just" moving to C? Let me warn you, you're walking into dangerous territory... C isn't nearly as simple as it sounds.
Reputation Points: 20
Solved Threads: 5
Newbie Poster
ffao is offline Offline
15 posts
since May 2007
Jun 8th, 2007
0

Re: Starting a project in python (Game and quiz)

Hi,

I also wanted to use other languages for a game etc that I wanted to do, so before I took on any language for what was needed I looked at how easy it would be for me to code my game in...C, C++, C#, Ruby, Perl, Java and while looking at these I accidently found Python...and thank goodness that I did! Some of these other codes can take 6 or 7 times longer to code with than Python and are 6 or 7 times more complicated to deal with! I've read where some math professors were involved in some complicated mathematical equations and dreaded using an equally complicated language like C to code it in...I've also read where people can take 7 or 8 months to code a project in say C where if they did the same thing in Python it would take only 2 weeks...so to say I'll just learn C etc like one is going to have an enjoyable steak lunch does not equate at all!

Believe me when I say that not every language is as easy to use as our beloved Python.

fredzik.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
fredzik is offline Offline
55 posts
since Feb 2007
Nov 27th, 2007
0

Re: Starting a project in python (Game and quiz)

wat does the quiz.dat file contain
Reputation Points: 10
Solved Threads: 1
Newbie Poster
atiqjaved is offline Offline
3 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: begginer
Next Thread in Python Forum Timeline: please help i am very desperate





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC