【问题标题】:Sublime Text 3 not accepting input()Sublime Text 3 不接受输入()
【发布时间】:2018-10-22 18:31:52
【问题描述】:

代码在 python shell 中按预期工作,通过空闲启动。通过 sublime text 运行文件时,它只会在控制台打印“请输入您的价格:”,而不在提交我的输入后运行其余代码。在控制台上按回车只会创建一个新行。在 python shell 中运行代码时,其余代码在将 int 提交到小费计算器后执行。

def calculateit():
     price = input("please enter your price: ")
     tip = int(price) * 0.25
     final = int(tip) + int(price)
     print ("since the price of your meal is " + str(price) + " your tip is " + str(tip))
     print ("the total cost of your meal is " + str(final))

calculateit()

【问题讨论】:

标签: python sublimetext3


【解决方案1】:

在 Sublime 中执行代码时,无法捕获输入。从技术上讲,控制台连接到stderrstdout,但不连接到stdin。所以不可能直接在 Sublime 中运行交互式程序。

但是在看到您的代码之后,出现了一个问题 - 为什么不使用用户 input 方法在第一次转换您的 price 变量,这样您就不需要多次从 @987654326 转换它@ 到 int,反之亦然。为了让您清楚这一点,请参阅以下代码,它与我认为完全相同但更聪明的方法。

def calculateit():
    price = int(input("please enter your price: "))
    tip = price * 0.25
    final = tip + price
    print (f"since the price of your meal is {price} your tip is {tip}" )
    print (f"the total cost of your meal is {final}" )

calculateit()

【讨论】:

  • 好的,谢谢。我只需要在空闲状态下运行它。你的问题的答案主要是因为我不知道更好。我正在逐行考虑。
  • @RobertN,如果对您有帮助,请标记答案。谢谢
猜你喜欢
  • 2020-02-09
  • 2018-08-14
  • 2019-04-06
  • 1970-01-01
  • 2016-02-07
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
相关资源
最近更新 更多