【问题标题】:I CANT END MY WHILE LOOP IN FUNTIONS USING PYTHON3我无法在使用 Python 3 的函数中结束我的 WHILE 循环
【发布时间】:2021-02-15 09:29:11
【问题描述】:
class Calculator:

    def get_input(self, get = input("Expression here press q to exit: ")):     
            pass
             
    def compute(self, res):
            pass
    def display(self):
          pass
calc = Calculator()   

while calc.get_input() != "q": 
    calc.get_input()
    calc.display()
    exit()

这是一个计算器,我只是因为这里的审查问题而删除了它的数据这么多代码 但输入必须是示例:435+422 你必须在输入中包含这样的操作,这是完成的,所以我现在的问题是 我不知道如何循环它。 输出必须是

-输入表达式或“q”退出:2+2

-输入表达式或“q”退出:1+2

-输入表达式或“q”退出:q -再见退出

我不知道

【问题讨论】:

  • 不要在get_input的参数中调用函数
  • 用您自己的话说,当您输入该输入时,导致calc.get_input() 等于"q" 的逻辑是什么?您对函数调用的工作原理有何理解?您熟悉return 关键字吗?您使用什么资源来学习 Python?
  • 你的函数没有 return 任何东西,所以它隐式返回 None
  • 用你自己的话说,Python 中类的目的是什么?你希望用class Calculator解决什么问题,而不是写普通的函数?
  • 对不起,我不熟悉这些,因为我是 python 的新手,我上个月才开始。我别无选择,只能上课,因为那是我教授的指示。这些功能名称已经给出,我们只需要完成并创建一个属性。但是非常感谢你!

标签: python python-3.x


【解决方案1】:

试试

class Calculator:
    def get_input(self):     
        user_input = input("Expression here press q to exit: ")
        return user_input

而不是在参数中调用输入

【讨论】:

    【解决方案2】:

    问题是你的get_input 没有返回任何东西。所以calc.get_input() 总是评估为None 试试:

        class Calculator:
    
        def get_input(self):
            return input("Expression here press q to exit: ")
                 
        def compute(self, res):
            pass
        def display(self):
            pass
    
    calc = Calculator()   
    while calc.get_input() != "q": 
        calc.get_input()
        calc.display()
        exit()
    

    【讨论】:

      猜你喜欢
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      • 2013-09-30
      • 2014-01-25
      • 2017-11-24
      相关资源
      最近更新 更多