Unexpected EOF error in python adventure game

143 views Asked by At

I am working on a Counter Strike: Global Offensive text based adventure for my final project in my high school computer science course.

I have tried everything asked many of my peers and ran through the python visualizer at least 10 times and I just can't seem to find the cause of my issue.

Please help me find the cause of this EOF error.

###############################################################################################################################################
# Programmer: Ethan
# Date: 29/5/15
# File name: CS-GO.py
# Description: This is a text based adventure game following the format and style of Counter Strike Global Offensive.
###############################################################################################################################################
import time
import math
import random
CT=1
T=2
money=800
hp=500
Round=1
class Players():
    def __init__(self,name,accuracy,hs,phrase):
        self.name=name
        self.accuracy=accuracy
        self.hs=hs
        self.phrase=phrase

    def __str__(self):
        return str(self.name)+" \t"+str(self.accuracy)+" \t\t"+str(self.hs)


player_1=Players("Ethan",45,82,"3...2.....1..... REKT")

player_2=Players("Adam",21,13,"Rush kitty kat MEOW MEOW!")

player_3=Players("Anson",3,5,"Ugh.......NO NO NO NO!")

player_4=Players("Greg",22,25,"HEIN SITZIZEN")                                  

player_5=Players("Connor",30,50,"Some of my fingers are on a trackpad..... the others.... well, you'll just have to ask your mother.")
##############
#Main Program#
##############
while True:
    try:

        print ("Welcome to text based CS:GO")
        time.sleep(2)
        print ("Please choose from one of the following players")
        time.sleep(.5)
        print ("## Name \tAccuracy\tHS%:")
        print("#1",player_1)
        print("#2",player_2)
        print("#3",player_3)
        print("#4",player_4)
        print("#5",player_5)
        player=int(input("Enter the corresponding #: "))



        if player ==1:
            ac=45
            hs=82


        elif player ==2:
            ac=21
            hs=13
        elif player ==3:
            ac=3
            hs=5
        elif player ==4:
            ac=22
            hs=25
        elif player ==5:
            ac=30
            hs=50

        game_mode=int(input("\nPlease press 1 in order to start a matchmaking game. \nYou can also press 2 to try your luck at some cases: "))
        if game_mode==1:
            print ("\nwelcome to matchmaking! Please follow the prompts in order to start your game")
            time.sleep(2)
            Map=int(input("\nHere at Volvo INC we run the standard competitive map pool.\nPlease press 1 for Dust 2 \nPlease press 2 for Inferno \nPlease press 3 for Mirage \nPlease press 4 for Cache \nPlease press 5 for Cobblestone \nPlease press 6 for Overpass: "))
            print ("\n Thank you for choosing Dust 2 ;)")
            time.sleep(.5)
            print ("\n Finding other silvers for you to play with")
            time.sleep(.5)
            print ("\n Finding the best potato('Server' for you to play on")
            time.sleep(.5)
            print ("\n Confirming match")
            time.sleep(2)
            print ("\n Starting game")
            time.sleep(5)
            Side=random.randint(0,2)
            if Side==1:
                Team=CT
                print ("\nWelcome to the Counter Terrorists")
                time.sleep(1.5)
                while hp >0:
                 print ("It is round #",Round)
                 print ("You have",money,"dollars")
                 menu=int(input("Would you like to buy something? y/n"))
                 if menu ==y:
                     print ("reky")

                 elif menu ==n:
                    print ("Ok then!")

                 else:
                     print ("that was an incorrect entry")



            else:
                Team=T
                print ("\nWelcome to the Terrorists")

        elif game_mode==2:
            print ("we both know you're not getting shit")
1

There are 1 answers

0
Dan Oberlam On

You're enclosing your game logic in a try statement, like so

while True:
    try:
        ...

This is problematic, because just a try doesn't really make sense - what happens if it fails? We use try statements when we want to handle exceptional circumstances - for example

try:
    function_that_can_raise_ValueError()
except ValueError as e:
    handle_that_ValueError(e)
else:
    behave_normally()
finally:
    clean_up_something()

You can have as many except blocks for your code as you would like, for as many Exception types as you would like. These should describe what should happen in the event of such an exception. You can also have a single, optional, else block that indicates what should happen if no Exception occurs. Lastly, there is the finally block which occurs after all the other code has run, regardless of whether or not an Exception (caught or otherwise) is raised.

It is required to have at least one except or finally block after your try block - otherwise that is syntactically invalid. Because you only have the try block, it reaches the end of file (EOF) searching for an except or finally, which is why you get that error.

From a cursory glance at your code it is unclear why you need the try - unless you have a specific reason for having it, I'd recommend getting rid of it altogether. If you do have a reason for having it, then you need an except or finally.

Read more on error handling (in Python 2.7) here.