0

So I am trying to make a function, plot, that will take f as a function ( I used cos in my shell), a and b as ranges with b greater than a.(B = 1 and A = 0 default), with n intervals, and m as the method of Riemann sum(center, left, or right.)

But the problem is that when I run the code and try to tye in right as a method, it does the following.

import numpy as np
import matplotlib.pyplot as plt

def plot(f, a = 0, b = 1, m = str('center'), n = 10):


    v = 0
    par = (b - a)/1000
    x = np.arange(a, b, par)
    plt.plot(x, f(x), 'black')
    w = (b - a)/n

##This is juts plotting the graph, does not need to be grouped

    if b < a:
        raise ValueError('Value b must be greater than a', b)

##Check if b is greater than a

    if m != str('center') or m != str('left') or m != str('right'):
        raise ValueError('Value m must be one of the following: center, left, or right', b)

##Check if  m is valid

    if n < 0:
        raise ValueError('N must be a positive integer', n)

##Check if n is positive and an integer

    if m == 'center':
        d = w /2
        x2 = np.arange(a + d , b + d , w)
        plt.bar(x2,f(x2), align = 'center', color = 'blue',\
                edgecolor = 'black', width = w, alpha = 0.5)
        print("The estimation using", m ,"is", v)
        plt.show()

##Does Mid Point

    if m == 'left':
        x2 = np.arange(a , b , w)
        plt.bar(x2,f(x2), align = 'edge', color = 'red',\
                edgecolor = 'black', width = w, alpha = 0.5)
        print("The estimation using", m ,"is", v)
        plt.show()

##Does Left Point

    if m == 'right':
        x2 = np.arange(a + w, b + w , w)
        plt.bar(x2,f(x2), align = 'edge', color = 'orange',\
                edgecolor = 'black', width = -w, alpha = 0.5)
        print("The estimation using", m ,"is", v)
        plt.show()


##Does Right Point
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    p2.plot(f)
  File "C:\Users\Vincent\AppData\Local\Programs\Python\Python37-32\Project2.py", line 21, in plot
    raise ValueError('Value m must be one of the following: center, left, or right', b)
ValueError: ('Value m must be one of the following: center, left, or right', 1)

This is the error message that I get. I didn't type in 1 for m, I typed in 1 for b but it is reading it somehow.

2
  • 2
    You do not have to do m=str('center') to create a string... Just using quotes will create a string: m='center' Commented Jul 18, 2019 at 17:58
  • BTW, I know this has been answered before, but I can't find one of the references. The question titles -- obviously -- do not refer to the actual problem. Otherwise, the poster would recognize and fix it before posting. Commented Jul 18, 2019 at 18:01

2 Answers 2

2

Your if logic is faulty:

if m != str('center') or m != str('left') or m != str('right'):

You need and connectors here; m must always be not equal to 2 or 3 of the words, so this condition is always True. Instead, try

if m not in ("center", "left", "right"):
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you soo much! This fixed it!
0

Change this block of code:

if m != str('center') or m != str('left') or m != str('right'):
        raise ValueError('Value m must be one of the following: center, left, or right', b)

To this:

if m not in ['center', 'left', 'right']:
        raise ValueError('Value m must be one of the following: center, left, or right', b)

Your first statement checks if m is not 'center', not 'right', or not'left', and since m can only be one, it will never be all three, therefore it will always raise the exception.

1 Comment

Thank you soo much! This fixed it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.