【问题标题】:I need help fixing an "Unexpected EOF error while parsing", please我需要帮助修复“解析时出现意外的 EOF 错误”,请
【发布时间】:2013-10-23 00:18:29
【问题描述】:

我是计算机科学专业的一年级学生,对自己在做什么一无所知,我真的需要一些帮助。我正在为一项任务编写一个程序,该程序将运行一个模拟,用户从三只股票中挑选一只,该程序将计算 10 年的收益/损失。

现在,我正在尝试实现收益和损失的随机部分,并且在解析错误时一直遇到意外的 eof。

删除最后两行代码后,错误消失。

我已经在网站上搜索了以前的解决方案,但正如我所说,我对编程非常陌生,而且我的 Python 不够流利,无法理解针对这些问题发布的解决方案。

import random


def main():
    starting_menu()
    choice_loop()

#Displays the starting menu 
def starting_menu():
    spendingCash=float(100.00)
    print("Current funds:", format(spendingCash, "0.2f"))
    print("Available investments")
    print("(1)Fly-By-Night investments (SCAMU): high risk, high potential returns")
    print("(2)Blue Chips INC (BCI): moderate risk, good yearly returns")
    print("(3)Slow and Steady Corp (BORE): mature industry, no risk but low returns")
    print("\nPlease enter a value from 1-3 only")
    print()

#Loop for if user selects a stock numbered other than 1-3
def choice_loop():
    chosen_stock=int(input("Stock Selection: "))

    while chosen_stock>3 or chosen_stock<1:
        print("Invalid choice, please enter number from 1-3 only")
        chosen_stock=int(input("Stock Selection: "))

    invest_chance=random.randrange(1,101)
    if chosen_stock==1:
        if invest_chance>=1 and invest_chance<=45:

感谢您的帮助。

【问题讨论】:

  • 请发布错误的完整堆栈跟踪 - 它包含有用的调试信息。
  • 另外,你用的是什么版本的python?
  • 只是一个旁注,在choice_loop() 中将int(input('')) 更改为int(raw_input())
  • 我使用的是 Python 3.3

标签: python parsing python-3.x eof


【解决方案1】:

您收到该错误是因为现在您在当前程序的底部有两个 if 语句,没有“主体”来说明如果满足这些语句的条件会发生什么。

Python 在 if 语句之后需要一些东西,因此它在解析代码时会遇到 EOF(文件结尾)。

如果有的话,你以前在那里有什么?

当您说错误消失时,您的意思是您是否使用 if 语句删除了最后两行?出于上述原因,这是有道理的。

【讨论】:

  • 回想起来,我对自己犯的一个简单错误感到震惊。编程有时令人沮丧,因为您认为不是错误的最微小的错误最终可能会导致大量问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-16
  • 1970-01-01
  • 2017-09-20
  • 2013-04-25
相关资源
最近更新 更多