0

我是计算机科学专业的一年级学生,对我在做什么一无所知,我真的需要一些帮助。我正在为一项任务编写一个程序,该程序将运行一个模拟,用户从三只股票中挑选一只,该程序将计算 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:

我很感激帮助。

4

1 回答 1

0

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

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

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

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

于 2013-10-23T00:58:32.280 回答