Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9256625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:59:21+00:00 2026-06-18T11:59:21+00:00

So I have the function integral(function, n=1000, start=0, stop=100) defined in nums.py : def

  • 0

So I have the function integral(function, n=1000, start=0, stop=100) defined in nums.py:

def integral(function, n=1000, start=0, stop=100):
    """Returns integral of function from start to stop with 'n' rectangles"""
    increment, num, x = float(stop - start) / n, 0, start
    while x <= stop:
        num += eval(function)
        if x >= stop: break
        x += increment
    return increment * num

However, my teacher (for my Programming class) wants us to create a separate program that gets the input using input() and then returns it. So, I have:

def main():
    from nums import integral # imports the function that I made in my own 'nums' module
    f, n, a, b = get_input()
    result = integral(f, n, a, b)
    msg = "\nIntegration of " + f + " is: " + str(result)
    print(msg)

def get_input():
    f = str(input("Function (in quotes, eg: 'x^2'; use 'x' as the variable): ")).replace('^', '**')
    # The above makes it Python-evaluable and also gets the input in one line
    n = int(input("Numbers of Rectangles (enter as an integer, eg: 1000): "))
    a = int(input("Start-Point (enter as an integer, eg: 0): "))
    b = int(input("End-Point (enter as an integer, eg: 100): "))
    return f, n, a, b

main()

When run in Python 2.7, it works fine:

>>> 
Function (in quotes, eg: 'x^2'; use 'x' as the variable): 'x**2'
Numbers of Rectangles (enter as an integer, eg: 1000): 1000
Start-Point (enter as an integer, eg: 0): 0
End-Point (enter as an integer, eg: 100): 100

Integration of x**2 is: 333833.5

However, in Python 3.3 (which my teacher insists we use), it raises an error in my integral function, with the same exact input:

Traceback (most recent call last):
  File "D:\my_stuff\Google Drive\documents\SCHOOL\Programming\Python\Programming Class\integration.py", line 20, in <module>
    main()
  File "D:\my_stuff\Google Drive\documents\SCHOOL\Programming\Python\Programming Class\integration.py", line 8, in main
    result = integral(f, n, a, b)
  File "D:\my_stuff\Google Drive\Modules\nums.py", line 142, in integral
    num += eval(function)
TypeError: unsupported operand type(s) for +=: 'int' and 'str'

In addition, integral by itself (in Python 3.3) works fine:

>>> from nums import integral
>>> integral('x**2')
333833.4999999991

Because of that, I believe the fault is in my program for my class… Any and all help is appreciated. Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:59:22+00:00Added an answer on June 18, 2026 at 11:59 am

    The issue you’re running into is that input works differently in Python 2 and Python 3. In Python 3, the input function works like the raw_input does in Python 2. Python 2’s input function is equivalent to eval(input()) in Python 3.

    You’re getting into trouble because of the quoteation marks you’re typing with the formula. When you type 'x**2' (with the quotes) as your formula when running on Python 2, the text gets evaled in the input function and you get a string with no quotation marks as the result. This works.

    When you give the same string to Python 3’s input function, it doesn’t do an eval, so the quotation marks remain. When you later eval the formula as part of your integral calculation, you get the string x**2 (without any quotation marks) as the result, not the value of x squared. This results in an exception when you try the string to 0.

    To fix this, I suggest either using just one version of Python, or putting the following code at the top of your file to get a Python 3 style input in both versions:

    # ensure we have Python 3 semantics from input, even in Python 2
    try:
        input = raw_input
    except NameError:
        pass
    

    Then just type in your formula without quotation marks and it should work correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have function LoadTempMovieList(), and need to load movies from sessionStorage. But it seems
I have function: char *zap(char *ar) { char pie[100] = INSERT INTO test (nazwa,
I have function Start() that is fired on ready. When I click on .ExampleClick
If you have an integral type t1 and a function getting a possibly smaller
I have function like this: function ypg_delete_img($id, $img) { $q = $this->ypg_get_one($id); $imgs =
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
I have function getCartItems in cart.js and I want to call that function in
I have function some_func_1 which will create an object of type some_type and will
I have function like this: function gi_insert() { if(!IS_AJAX){ $this->load->library('form_validation'); $this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags'); $this->form_validation->set_rules('body',
i have function to send mail with attachment to microsoft exchange server. My problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.