python冒险游戏中出现意外的EOF错误

时间:2015-06-08 15:43:06

标签: python eof

我正在为我在高中计算机科学课程中的最终项目进行反恐精英:全球攻势性文本冒险。

我已经尝试了很多同事的问题并且至少跑了10次python可视化工具,而我似乎无法找到问题的原因。

请帮我找出导致此EOF错误的原因。

###############################################################################################################################################
# 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 个答案:

答案 0 :(得分:1)

您将游戏逻辑封装在try语句中,如此

while True:
    try:
        ...

这是有问题的,因为只有try才真正有意义 - 如果失败会发生什么?当我们想要处理特殊情况时,我们使用try语句 - 例如

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

您可以根据需要为代码添加尽可能多except个代码块,因为您可以根据需要添加Exception个类型。这些应描述在发生此类异常时应该发生的情况。您还可以使用一个可选的else块来指示如果 Exception发生时会发生什么。最后,在所有其他代码运行之后会出现finally块,无论是否引发Exception(捕获或其他)。

必需except阻止后至少有一个finallytry阻止 - 否则语法无效。由于您只有try块,因此会到达文件末尾(EOF)搜索exceptfinally,这就是您收到错误的原因。

粗略地看一下您的代码,目前还不清楚为什么需要try - 除非您有特定的理由,否则我建议完全摆脱它。如果您确实有理由,那么您需要exceptfinally

阅读有关错误处理的更多信息(在Python 2.7中)here