Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何改进Think Python的代码,练习7.4 eval和loop_Python - Fatal编程技术网

如何改进Think Python的代码,练习7.4 eval和loop

如何改进Think Python的代码,练习7.4 eval和loop,python,Python,任务: 编写一个名为eval\u loop的函数,迭代提示用户,获取结果输入,并使用eval()对其求值,然后打印结果 它应该一直持续,直到用户输入“完成”,然后返回它计算的最后一个表达式的值 我的代码: import math def eval_loop(m,n,i): n = raw_input('I am the calculator and please type: ') m = raw_input('enter done if you would like to qu

任务:

编写一个名为
eval\u loop
的函数,迭代提示用户,获取结果输入,并使用
eval()
对其求值,然后打印结果

它应该一直持续,直到用户输入
“完成”
,然后返回它计算的最后一个表达式的值

我的代码:

import math

def eval_loop(m,n,i):
    n = raw_input('I am the calculator and please type: ')
    m = raw_input('enter done if you would like to quit! ')
    i = 0   
    while (m!='done' and i>=0):
        print eval(n)
        eval_loop(m,n,i)
        i += 1
        break;

eval_loop('','1+2',0)
我的代码无法返回它计算的最后一个表达式的值

三点意见:

  • 为此使用递归意味着您最终将达到系统递归极限,迭代可能是一种更好的方法(也是您被要求采用的方法!)
  • 如果要
    返回
    评估结果
    ,则需要分配该结果;及
  • 我不知道
    I
    在您的代码中的作用,但它似乎没有任何帮助 考虑到这些,简要概述如下:

    def eval_loop():
        result = None
        while True:
            ui = raw_input("Enter a command (or 'done' to quit): ")
            if ui.lower() == "done":
                break
            result = eval(ui)
            print result
        return result
    

    对于一个更健壮的函数,考虑在<<代码> > 中处理包装>代码> EVA/COD>,并处理它所产生的任何错误。p> 三点意见:

    import math
    def eval_loop():
        while True:
            x=input('Enter the expression to evaluate: ')
            if x=='done':
                break
            else:
                y=eval(x)
                print(y)
        print(y)
    
    eval_loop()
    
  • 为此使用递归意味着您最终将达到系统递归极限,迭代可能是一种更好的方法(也是您被要求采用的方法!)
  • 如果要
    返回
    评估结果
    ,则需要分配该结果;及
  • 我不知道
    I
    在您的代码中的作用,但它似乎没有任何帮助 考虑到这些,简要概述如下:

    def eval_loop():
        result = None
        while True:
            ui = raw_input("Enter a command (or 'done' to quit): ")
            if ui.lower() == "done":
                break
            result = eval(ui)
            print result
        return result
    

    对于一个更健壮的函数,考虑在<<代码> > 中处理包装>代码> EVA/COD>,并处理它所产生的任何错误。p> 三点意见:

    import math
    def eval_loop():
        while True:
            x=input('Enter the expression to evaluate: ')
            if x=='done':
                break
            else:
                y=eval(x)
                print(y)
        print(y)
    
    eval_loop()
    
  • 为此使用递归意味着您最终将达到系统递归极限,迭代可能是一种更好的方法(也是您被要求采用的方法!)
  • 如果要
    返回
    评估结果
    ,则需要分配该结果;及
  • 我不知道
    I
    在您的代码中的作用,但它似乎没有任何帮助 考虑到这些,简要概述如下:

    def eval_loop():
        result = None
        while True:
            ui = raw_input("Enter a command (or 'done' to quit): ")
            if ui.lower() == "done":
                break
            result = eval(ui)
            print result
        return result
    

    对于一个更健壮的函数,考虑在<<代码> > 中处理包装>代码> EVA/COD>,并处理它所产生的任何错误。p> 三点意见:

    import math
    def eval_loop():
        while True:
            x=input('Enter the expression to evaluate: ')
            if x=='done':
                break
            else:
                y=eval(x)
                print(y)
        print(y)
    
    eval_loop()
    
  • 为此使用递归意味着您最终将达到系统递归极限,迭代可能是一种更好的方法(也是您被要求采用的方法!)
  • 如果要
    返回
    评估结果
    ,则需要分配该结果;及
  • 我不知道
    I
    在您的代码中的作用,但它似乎没有任何帮助 考虑到这些,简要概述如下:

    def eval_loop():
        result = None
        while True:
            ui = raw_input("Enter a command (or 'done' to quit): ")
            if ui.lower() == "done":
                break
            result = eval(ui)
            print result
        return result
    

    对于一个更健壮的函数,考虑在<<代码> > 中处理包装>代码> EVA/COD>,并处理它所产生的任何错误。p> 这是我想出的代码。首先,使用If、else条件来理解代码流。然后使用while循环编写它

    import math
    def eval_loop():
        while True:
            x=input('Enter the expression to evaluate: ')
            if x=='done':
                break
            else:
                y=eval(x)
                print(y)
        print(y)
    
    eval_loop()
    
        import math
        #using the eval function
        """eval("") takes a string as a variable and evaluates it
        Using (If,else) Conditionals"""
    
        def eval_(n):
            m=int(n)
            print("\nInput n = ",m)
            x=eval('\nmath.pow(m,2)')
            print("\nEvaluated value is = ", x)
        def run():
            n= input("\nEnter the value of n = ") 
            if n=='done' or n=='Done':
                print("\nexiting program")
                return
            else:
                 eval_(n)
            run() # recalling the function to create a loop
        run()
    
    现在使用While循环执行相同的操作

        "using eval("") function using while loop"
    
        def eval_1():
            while True:
                n=input("\nenter the value of n = ") #takes a str as input
                if n=="done" or n=="Done": #using string to break the loop
                    break
                m=int(n) # Since we're using eval to peform a math function.
                print("\n\nInput n = ",m) 
                x=eval('\nmath.pow(m,2)') #Using m to perform the math
                print("\nEvaluated value is " ,x)        
        eval_1()
    

    这是我想出的密码。首先,使用If、else条件来理解代码流。然后使用while循环编写它

        import math
        #using the eval function
        """eval("") takes a string as a variable and evaluates it
        Using (If,else) Conditionals"""
    
        def eval_(n):
            m=int(n)
            print("\nInput n = ",m)
            x=eval('\nmath.pow(m,2)')
            print("\nEvaluated value is = ", x)
        def run():
            n= input("\nEnter the value of n = ") 
            if n=='done' or n=='Done':
                print("\nexiting program")
                return
            else:
                 eval_(n)
            run() # recalling the function to create a loop
        run()
    
    现在使用While循环执行相同的操作

        "using eval("") function using while loop"
    
        def eval_1():
            while True:
                n=input("\nenter the value of n = ") #takes a str as input
                if n=="done" or n=="Done": #using string to break the loop
                    break
                m=int(n) # Since we're using eval to peform a math function.
                print("\n\nInput n = ",m) 
                x=eval('\nmath.pow(m,2)') #Using m to perform the math
                print("\nEvaluated value is " ,x)        
        eval_1()
    

    这是我想出的密码。首先,使用If、else条件来理解代码流。然后使用while循环编写它

        import math
        #using the eval function
        """eval("") takes a string as a variable and evaluates it
        Using (If,else) Conditionals"""
    
        def eval_(n):
            m=int(n)
            print("\nInput n = ",m)
            x=eval('\nmath.pow(m,2)')
            print("\nEvaluated value is = ", x)
        def run():
            n= input("\nEnter the value of n = ") 
            if n=='done' or n=='Done':
                print("\nexiting program")
                return
            else:
                 eval_(n)
            run() # recalling the function to create a loop
        run()
    
    现在使用While循环执行相同的操作

        "using eval("") function using while loop"
    
        def eval_1():
            while True:
                n=input("\nenter the value of n = ") #takes a str as input
                if n=="done" or n=="Done": #using string to break the loop
                    break
                m=int(n) # Since we're using eval to peform a math function.
                print("\n\nInput n = ",m) 
                x=eval('\nmath.pow(m,2)') #Using m to perform the math
                print("\nEvaluated value is " ,x)        
        eval_1()
    

    这是我想出的密码。首先,使用If、else条件来理解代码流。然后使用while循环编写它

        import math
        #using the eval function
        """eval("") takes a string as a variable and evaluates it
        Using (If,else) Conditionals"""
    
        def eval_(n):
            m=int(n)
            print("\nInput n = ",m)
            x=eval('\nmath.pow(m,2)')
            print("\nEvaluated value is = ", x)
        def run():
            n= input("\nEnter the value of n = ") 
            if n=='done' or n=='Done':
                print("\nexiting program")
                return
            else:
                 eval_(n)
            run() # recalling the function to create a loop
        run()
    
    现在使用While循环执行相同的操作

        "using eval("") function using while loop"
    
        def eval_1():
            while True:
                n=input("\nenter the value of n = ") #takes a str as input
                if n=="done" or n=="Done": #using string to break the loop
                    break
                m=int(n) # Since we're using eval to peform a math function.
                print("\n\nInput n = ",m) 
                x=eval('\nmath.pow(m,2)') #Using m to perform the math
                print("\nEvaluated value is " ,x)        
        eval_1()
    

    此方法将首先对用户输入的内容运行eval,然后将该输入添加到名为b的新变量中。 当用户输入单词“done”时,它将按照练习的要求打印新创建的变量b

    def eval_loop():
        while True:
            a = input("enter a:\n")
            if a == "done":
                print(eval(b)) # if "done" is entered, this line will print variable "b" (see comment below)
                break
            print(eval(a))
            b = a # this adds the last evaluated to a new variable "b"
    eval_loop()
    

    此方法将首先对用户输入的内容运行eval,然后将该输入添加到名为b的新变量中。 当用户输入单词“done”时,它将按照练习的要求打印新创建的变量b

    def eval_loop():
        while True:
            a = input("enter a:\n")
            if a == "done":
                print(eval(b)) # if "done" is entered, this line will print variable "b" (see comment below)
                break
            print(eval(a))
            b = a # this adds the last evaluated to a new variable "b"
    eval_loop()
    

    此方法将首先对用户输入的内容运行eval,然后将该输入添加到名为b的新变量中。 当用户输入单词“done”时,它将按照练习的要求打印新创建的变量b

    def eval_loop():
        while True:
            a = input("enter a:\n")
            if a == "done":
                print(eval(b)) # if "done" is entered, this line will print variable "b" (see comment below)
                break
            print(eval(a))
            b = a # this adds the last evaluated to a new variable "b"
    eval_loop()
    

    此方法将首先对用户输入的内容运行eval,然后将该输入添加到名为b的新变量中。 当用户输入单词“done”时,它将按照练习的要求打印新创建的变量b

    def eval_loop():
        while True:
            a = input("enter a:\n")
            if a == "done":
                print(eval(b)) # if "done" is entered, this line will print variable "b" (see comment below)
                break
            print(eval(a))
            b = a # this adds the last evaluated to a new variable "b"
    eval_loop()
    

    我希望这本书也告诉你,除了像这样的玩具程序之外,你永远不应该使用eval。。。有关此问题的讨论,请参阅或其他多个SO问题。TL;DR版本:这是一种安全风险,速度慢,您将失去可读性/IDE支持(突出显示、重构)等。谢谢!这只是一本面向初学者甚至业余爱好者的书。我希望这本书也能告诉你,除了像这样的玩具程序之外,你不应该使用eval。。。有关此问题的讨论,请参阅或其他多个SO问题。TL;DR版本:这是一种安全风险,速度慢,您将失去可读性/IDE支持(突出显示、重构)等。谢谢!这只是一本面向初学者甚至业余爱好者的书。我希望这本书也能告诉你,除了像这样的玩具程序之外,你不应该使用eval。。。有关此问题的讨论,请参阅或其他多个SO问题。TL;DR版本:这是一种安全风险,速度慢,您将失去可读性/IDE支持(突出显示、重构)等。谢谢!这只是一本适合初学者甚至是初学者的书