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()
5 4563
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): -
game=os.listdir(os.path.join(os.getcwd(),"Question"))
-
line 28 - I believe in this loop you're looking for the following: -
for i, s in enumerate(game):
-
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
pls can you tell me how to use code tags for posting?, thanks
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 []
-
-
import os
-
-
while True:
-
print"Welcome to trivia game!"
-
print
-
print "Choose a game to play:"
-
print
-
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
-
-
-
print"Welcome to trivia game!"
-
print
-
print "Choose a game to play:"
-
print
-
print "1- IQ Test.trv"
-
game=os.listdir(os.path.join(os.getcwd()+"Questions"))
-
-
i=0
-
for i,s in enumerate(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.path.join(os.getcwd()+"Questions"+game[choice],"r"))
-
score=0
-
-
-
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."
-
-
-
if results[answer]==correct[0]:
-
print "n\Right"
-
score += float(points)
-
else:
-
print "n\Wrong" + explanation
-
-
print "Score:", score, "\n\n"
-
question=myFile.readline()
-
-
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: -
# import things first
-
-
# function definitions here
-
def func1():
-
# func1 details...
-
def func2():
-
# func2 details...
-
-
# here's your main function
-
def main():
-
# Put your main execution code here
-
-
if __name__ == '__main__':
-
# Perform any initialization here then call your main function
-
main()
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |