【问题标题】:python rounds automatically when i work trhough spyder, but not when i type in direcly in console =S当我通过 spyder 工作时,python 会自动舍入,但当我直接在控制台中输入时不会 =S
【发布时间】:2014-07-17 12:01:10
【问题描述】:

所以我正在创建一类雇员,并且我想创建一个返回总工作时间的函数

试试 1、通过spyder运行这个 2. 在控制台中运行

给出不同的结果:S

class Employee(object):      
    def __init__(self, wage, wage1, hours_accounting=[]):
        self.wage=wage
        self.wage1=wage1
        self.hours_accounting=hours_accounting
    def work(self):               
        A=H,M=input('when did you start work today? format hh,mm')
        B=H1,M1=input('when did you stop work today? format hh,mm')     
        total_time_in_hours=H1-H + (M1-M)/60              
        return total_time_in_hours

amanda=Employee(100, 50)
amanda.work()
07,15
21,00

#

为什么?提前致谢!

【问题讨论】:

    标签: python rounding spyder


    【解决方案1】:

    (Spyder dev here) 在 Spyder 2.3.0 之前,我们在启动控制台时运行了这段代码:

    from __future__ import division
    

    这使得 2/3 之类的东西返回 0.6666 而不是 0(这是 Python 2 中的正常行为)。

    要改变这一点,您有三个选择:

    1. 更新到2.3.0

    2. 如果您使用的是 Python 控制台,则需要转到

      Preferences > Console > Advanced Settings > PYTHONSTARTUP replacement

      并选择名为

      的选项

      Default PYTHONSTARTUP script.

    3. 如果你使用的是 IPython 控制台,你需要去

      Preferences > IPython Console > Graphics

      并取消选择调用的选项

      Automatically load Pylab and NumPy modules

    【讨论】:

    • 如果真正的除法处于活动状态,则不应禁用它,因为这是 Python 3.0 的行为,最好今天为最终转换准备代码。只需明确使用截断除法“//”,您就会以可移植的方式获得旧行为。
    • 我们停用了它,因为它在新手中产生了混乱(就像 OP 报告的那样):“它在 Python 中以一种方式工作,但在 Spyder 中它的工作方式不同”。恕我直言,这样做的真正便携方式是显式调用from __future__ import division,而不是我们强迫用户使用=)
    猜你喜欢
    • 2022-01-16
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2021-09-27
    • 2017-06-23
    • 1970-01-01
    相关资源
    最近更新 更多