473,407 Members | 2,676 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,407 software developers and data experts.

Trivia Game

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
5 4563
jlm699
314 100+
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
pls can you tell me how to use code tags for posting?, thanks
Mar 13 '08 #3
jlm699
314 100+
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
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
314 100+
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

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

Similar topics

138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
2
by: Tim Simmons | last post by:
I am stumped. I encoded the action = of my form using GET and I can't seem to get the property/value stuff from it using a JavaScript script I got from the web. I want to create a trivia game...
1
by: fowle040 | last post by:
I underlined and bold print my files. I need to know how to make this code into a working game. The object of the game is to have two players 1- belle and 2-beast. I want them to lose and gain...
7
by: Gasten | last post by:
Hello. The last weeks I've been coding a roguelike (you know, like nethack) in python using the nCurses library. Some week ago I ran into a problem: When I made the object for messagebar-output, I...
5
by: Kraken | last post by:
Hi, i have a bit of a problem here. I have an assignment to do an animal guessing game using an original database and updating it as the user enters new animals in it. The program enters the file...
0
by: Shawn Minisall | last post by:
For my final project, I'm trying to do a GUI based game similar to are you smarter then a 5th grader. I've been working on it and am stuck with some code someone helped me with to randomize the...
2
by: LilMeechc20 | last post by:
Hello, I have a group assignment that I have to do. We have to write a Tic Tac Toe game. One person in my group has managed to write the code for a multiplayer (human -vs- human) game and I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.