【问题标题】:Python file crashesPython文件崩溃
【发布时间】:2022-06-20 13:14:56
【问题描述】:

(这是我发现@gooney 没有回答的问题)

今年夏天刚开始我的第一个计算机科学课程,需要一些帮助。

我们的任务是制作一个股票交易程序(无关紧要),我在两件事上遇到了麻烦。每当我从文件资源管理器中打开 .py 文件时,它会在第 6 次输入后崩溃。每当我直接从 cmd(python“文件名”)或在 IDLE 中运行它时,它都可以正常工作,所以我很困惑。

任何帮助将不胜感激。这是我的代码以及它在 CMD 中的样子。 [代码][2]

# This program will be used to determine
# Calculations for a stock transaction

# The inputs
Shares_Bought = int(input('Enter shares purchased: '))
SharePrice_B = float(input('Enter purchase price of shares: '))
B_Commission = float(input('Enter broker buy commission: '))
Shares_Sold = int(input('Enter shares sold: '))
SharePrice_S = float(input('Enter sell price of shares: '))
S_Commission = float(input('Enter broker sell comission: '))

#CONSTANTS?
TOTAL_PURCHASE = Shares_Bought * SharePrice_B
TOTAL_SALE = Shares_Sold * SharePrice_S
TOTAL_SALES_COMMISSION = (Shares_Bought * SharePrice_B) * (B_Commission / 100) + (Shares_Sold * SharePrice_S) * (S_Commission / 100)



# The outputs 
print('Alex Partida Stock Transaction App')
print(f'Joe Paid: ${Shares_Bought * SharePrice_B:,.2f}')
print(f'Purchase Commission: ${(Shares_Bought * SharePrice_B) * (B_Commission / 100):,.2f}')        # Need to make this shorter/split into more lines
print(f'Joe sold at: ${Shares_Sold * SharePrice_S:,.2f}')
print(f'Sales Commission: ${(Shares_Sold * SharePrice_S) * (S_Commission / 100):,.2f}')
print(f"Joe's Profit: ${(TOTAL_SALE - TOTAL_PURCHASE) - TOTAL_SALES_COMMISSION:,.2f}") 

【问题讨论】:

    标签: python


    【解决方案1】:

    问题是当 python 文件完成其代码时,它会退出。在 cmd 和 ide 中,这没有问题,因为程序会继续运行并向您显示输出。但是如果我们单独运行该文件,该文件将在运行其代码后退出,因为它没有任何事情要做。使用简单的 input("Press enter to exit...") 为了达到教授在这种情况下所做的相同效果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-04
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多