By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,768 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,768 IT Pros & Developers. It's quick & easy.

Trivia Game

P: 3
Help!!!! Im new using python, currently writing a program which tests one game, IQ test. When the module is run, the program should ask user to choose the game to start. Also, I'm using Try...Except statements, and os file operator.
My code needs some adjustments, most of all at the end. Please give some tips!!!


import os

while True:
print"Welcome to trivia game!"
print "0-Choose a game to play:"
print "1- IQ Test.trv"

s=raw_input()
if s==1:
def open_file(file_name,mode):
"""Open a file."""
try:
choice=float(raw_input("Choice:"))
the_file=open(file_name,mode)
except(IOError),e:
print "Unable to open the file", file_name,"Ending program.\n",e
raw_input("\n\nPress the enter key to exit")
else:
return the_file

#Directory
print"Welcome to trivia game!"
print
print "0-Choose a game to play:"
print
print "1- IQ Test.trv"
game=os.listdir(os.getcwd()+"Question")

i=0
for i in game:
print i, s
i=i+1

while True:
try:
choice=float(raw_input("Choice:"))
if choice>=0 and choice<=len(game):
break
except(ValueError):
print "Not valid."



myFile=open_file(os.listdir(os.getcwd()+"Question" +game[choice],"r")
score=0

#Beginning of the trivia game
question=myFile.readline()
while choice != "":
answer1=myFile.readline()
answer2=myFile.readline()
answer3=myFile.readline()
answer4=myFile.readline()
explanation=myFile.readline()
points=float(myFile.readline())

print question

print "1- " + answer1
print "2- " + answer2
print "3- " + answer3
print "4- " + answer4

results=["answer1","answer2", "answer3", "answer4"]

while True:
try:
answer= raw_input("What is your answer?:")
if answer>=1 and answer<=4
break
except(ValueError):
print "Not valid answer."

#Results
if results[answer]==correct[0]:
print "n\Right"
score += points
else:
print "n\Wrong" + explanation

print "Score:", score, "\n\n"
question=myFile.readline()

if s!=1:
print "Unable to open the file"

os.close()
Mar 12 '08 #1
Share this Question
Share on Google+
5 Replies


jlm699
100+
P: 314
Please try to use [code] tags when posting, it makes it much more readable...

There are a number of syntax errors...
line 45 - missing closing parenthesis
line 69 - missing colon at end of if statement

I'm not sure what you're trying to do here though, as when the 'game' starts it just keeps asking what you would like to do (stuck in the while True: loop)

There is no way to exit the loop, and every time the user enters '1', it simply redefines the function open_file.

Other suggestions:
line 25 - This line should be modified as the following (and line 42 should be modified in a similar way):
Expand|Select|Wrap|Line Numbers
  1. game=os.listdir(os.path.join(os.getcwd(),"Question"))
  2.  
line 28 - I believe in this loop you're looking for the following:
Expand|Select|Wrap|Line Numbers
  1. for i, s in enumerate(game):
  2.  
Perhaps the structuring of this program was lost when you didn't use the [code] tags, so perhaps try reposting and fixing the indentation and maybe we can help further
Mar 12 '08 #2

P: 3
pls can you tell me how to use code tags for posting?, thanks
Mar 13 '08 #3

jlm699
100+
P: 314
pls can you tell me how to use code tags for posting?, thanks
When you hit reply on the right side of the editing box there is a message box that reads: REPLY GUIDELINES and the third bullet shows how to use code tags. You basically have an opening [code] tag (tip: use code=python inthis tag to designate the code as python code... then you have a closing tag that is /code surrounded by []
Mar 13 '08 #4

P: 3
Expand|Select|Wrap|Line Numbers
  1.  
  2. import os
  3.  
  4. while True:
  5.     print"Welcome to trivia game!"
  6.     print
  7.     print "Choose a game to play:"
  8.     print
  9.     print "1- IQ Test.trv"
  10.  
  11.     s=raw_input()
  12.     if s==1:
  13.  
  14.  
  15.         def open_file(file_name,mode):
  16.             """Open a file."""
  17.             try:
  18.                 choice=float(raw_input("Choice:"))
  19.                 the_file=open(file_name,mode)
  20.             except(IOError),e:
  21.                 print "Unable to open the file", file_name,"Ending program.\n",e
  22.                 raw_input("\n\nPress the enter key to exit")
  23.             else:
  24.                 return the_file
  25.  
  26.  
  27. print"Welcome to trivia game!"
  28. print
  29. print "Choose a game to play:"
  30. print
  31. print "1- IQ Test.trv"
  32. game=os.listdir(os.path.join(os.getcwd()+"Questions"))
  33.  
  34. i=0
  35. for i,s in enumerate(game):
  36.     print i,s
  37.     i=i+1
  38.  
  39.  
  40.  
  41. while True:
  42.     try:
  43.         choice=float(raw_input("Choice:"))
  44.         if choice>=0 and choice<len(game):
  45.             break
  46.     except(ValueError):
  47.         print "Not valid."
  48.  
  49.  
  50.  
  51. myFile=open_file(os.listdir(os.path.join(os.getcwd()+"Questions"+game[choice],"r"))  
  52. score=0
  53.  
  54.  
  55. question=myFile.readline()
  56. while choice != "":
  57.     answer1=myFile.readline()
  58.     answer2=myFile.readline()
  59.     answer3=myFile.readline()
  60.     answer4=myFile.readline()
  61.     explanation=myFile.readline()
  62.     points=float(myFile.readline())
  63.  
  64.     print question
  65.  
  66.     print "1- " + answer1
  67.     print "2- " + answer2
  68.     print "3- " + answer3
  69.     print "4- " + answer4
  70.  
  71.     results=["answer1","answer2", "answer3", "answer4"]
  72.  
  73.     while True:
  74.         try:
  75.             answer= raw_input("What is your answer?:")
  76.             if answer>=1 and answer<=4:
  77.                 break
  78.         except(ValueError):
  79.             print "Not valid answer."
  80.  
  81.  
  82. if results[answer]==correct[0]:
  83.     print "n\Right"
  84.     score += float(points)
  85. else:
  86.     print "n\Wrong" + explanation
  87.  
  88. print "Score:", score, "\n\n"
  89. question=myFile.readline()
  90.  
  91.  
Mar 13 '08 #5

jlm699
100+
P: 314
You fixed the two syntax errors, but you're still putting an infinite loop at the very beginning of your program that keeps re-defining a function and nothing else. Take the function definition out of that loop and get rid of that loop, as it does nothing. The rest of your code looks like it's supposed to go there, however it seems your structure is all messed up. Try something similar to this:
Expand|Select|Wrap|Line Numbers
  1. # import things first
  2.  
  3. # function definitions here
  4. def func1():
  5.     # func1 details...
  6. def func2():
  7.     # func2 details...
  8.  
  9. # here's your main function
  10. def main():
  11.     # Put your main execution code here
  12.  
  13. if __name__ == '__main__':
  14.     # Perform any initialization here then call your main function
  15.     main()
Mar 13 '08 #6

Post your reply

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