Question

In: Computer Science

Please what is wrong with this two code that is showing error message when run them?...

Please what is wrong with this two code that is showing error message when run them?

>>>data =[1,2,3,4] >>>reduce(lambda x,y:x+y,data) #Produce the sum 10 >>> reduce(lambda x,y:x*y, data) # Produce the product 24

___________________________________________________________________________________________________________-

def sum(lower,upper): “””Returns the sum of the numbers from lower to upper.””” if lower> upper: return 0 else: return reduce(lambda x,y:x+y, range(lower, upper+1))

Solutions

Expert Solution

// Screenshot of the code

// Sample output

// Code to copy

sum.py

from functools import reduce

def sum(lower,upper):
if lower> upper:
return 0
else:
return reduce(lambda x,y:x+y, range(lower, upper+1))

Explanation:

You need to add the below module to your python program.

from functools import reduce


Related Solutions

using this code under, I want when the user input only letter q an error message...
using this code under, I want when the user input only letter q an error message appears that there is no second smallest, how I can do that? #include <iostream> #include <sstream> #include <vector> #include <algorithm> using namespace std; int secondSmallestNumber(vector<int> &I); int main() {    cout << "Enter the numbers in random order: (close by entering q)" << endl;    vector<int> I;    int input;    stringstream ss;    string str;    bool flag = false;    while (!flag) {        getline(cin, str);        ss.clear();        ss << str;...
What caused the following error message to appear in Microsoft access do not merely state the name of the error indicate why the error occurred and what the user was doing when this message appeared?
What caused the following error message to appear in Microsoft access do not merely state the name of the error indicate why the error occurred and what the user was doing when this message appeared?
What is the error message that occurs when the manager sends Set_Request to a read-only object?...
What is the error message that occurs when the manager sends Set_Request to a read-only object? Also, list out any three error messages in SNMP v1.
What is the error message that occurs when the manager sends Set_Request to a read-only object?...
What is the error message that occurs when the manager sends Set_Request to a read-only object? Also, list out any three error messages in SNMP v1.
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
What am i doing wrong. I want for the program to run through then when it...
What am i doing wrong. I want for the program to run through then when it gets to "do you want to play again?enter yes or no" if they choose yes I want it to run again if they choose no then end. def play(): print("Welcome to the Game of Life!") print("A. Banker") print("B. Carpenter") print("C. Farmer") user_input = input("What is your occupation? ").upper() if user_input == "A": money = 100 elif user_input == "B": money = 70 elif user_input...
What does it mean when you get an error message running SPSS forbinary linear regression...
What does it mean when you get an error message running SPSS for binary linear regression that states the dependent variable has more than two non-missing values. For logistic regression, the dependent variable value must assume exactly two values on the cases being processed. What do I need to do to fix this issue?
What is wrong with the following code? Give two effective ways to fix it. 1 import...
What is wrong with the following code? Give two effective ways to fix it. 1 import javax.swing.*; 2 import java.awt.event.*; 3 4 public class H2ClassJ extends JFrame { 5    public static final long serialVersionUID = 22; 6 7 public H2ClassJ () { 8      addMouseListener (new MouseListener () { 9    public void mouseClicked (MouseEvent e) {} 10     }); 11   } // end constructor 12 13 } // end class H2ClassJ
It have a lot of error. please see it and solve it for me. run it...
It have a lot of error. please see it and solve it for me. run it please. Thank you a lot. #define MAXWORDLENGTH 20 #include <stdlib.h> #include <stdio.h> #include <string.h> #include "getline.inc" struct entry { char name[81]; }; struct entry part; struct tnode { char *name; int record_number; struct tnode *left; struct tnode *right; }; void lookup(struct tnode *root,char *s); void treeprint2(struct tnode *root,char *searchkey); int c; int n; FILE *stream; struct tnode *root, *tree(struct tnode *, char *, int),*dtree(struct...
When a firm operates in the short run, what must be true for them to choose...
When a firm operates in the short run, what must be true for them to choose to shut down (produce 0 output)? What must be true for the firm to produce a non-zero level of output? How do they choose what level of output to produce?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT