• Register
1 vote
2.9k views

Problem:

I recently started learning python programming. This is my very first attempt to write a code run the program. I tried to compile my program but it throwing me back an error.

def multiply(a, b):
  a * b

What should I do now?

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution:

As per your question, I understand that you want to make a function in your program that will multiply two different numbers. You need to keep in mind if you define a function it has to have a return value otherwise, it doesn’t make any sense.

So you have to have a return value in your function right this way:

def multiply(a, b):
  return a * b

Now, this should compile and run error-free. Thanks.

13 9 6
94,240 points
0 votes

Solution:

In case it is a function, it requires to return something. Else, running it is kind of useless.

So you possibly require to say:

def multiply(a, b):
  return a * b

You possibly want to read more about functions in Python and at the time this would make sense (passing by reference, for example). This can be a good starting point: Python functions.

There is no return value, the code will be OK

def multiply(a, b):
   return a * b

For example 111 x 101 gives:

 111
  x101
------
   111
  000
 111
 -----
 11011
 =====

We can illustrate 101 as x2+1x2+1 and 110 as x2+xx2+x

In next move we multiple them together to give:

(x2+1)×(x2+x)(x2+1)×(x2+x)

(x4+x3+x2+x)(x4+x3+x2+x)

which can be illustrated as 11110

For example in case we exercise the example on Page 6 [here] of 84 x 13 (and where ** represents "to the power of")

84x13 = ((2**6+ 2**4+ 2**2)x(2**3+ 2**2+ 2**0)) (mod 2)
      = (2**9+ 2**8+ 2**7+2**1+ 2**6+ 2**5+ 2**1 + 2**4+ 2**2) (mod 2)
      = (2**9+ 2**8+ 2**7+ 2**5+ 2**2) (mod 2)

which is 1110100100 [Calc]

The performing out in its proper form is:

84x13 = ((26+24+22)×(23+22+20))(mod2)
=(29+28+26+27+26+24+25+24+22)(mod2)
=(29+28+27+25+22)(mod2)

I have exercise a simple bit-shift operation and X-OR to implement the method:

import struct
import sys

val1=1
val2=1


if (len(sys.argv)>1):
        val1=str(sys.argv[1])

if (len(sys.argv)>2):
        val2=str(sys.argv[2])

def reverse(text):

    lst = []
    count = 1

    for i in range(0,len(text)):

        lst.append(text[len(text)-count])
        count += 1

    lst = ''.join(lst)
    return lst


def showpoly(a):
	str1 = ""
	nobits = len(a)


    	for x in range (0,nobits-2):
		if (a[x] == '1'):
			if (len(str1)==0):
				str1 +="x**"+str(nobits-x-1)	
			else: 
				str1 +="+x**"+str(nobits-x-1)

	if (a[nobits-2] == '1'):
		if (len(str1)==0):
			str1 +="x"
		else:
			str1 +="+x"

	if (a[nobits-1] == '1'):
		str1 +="+1"

	print str1;
	
def multiply(a,b):
	bit1 = int(a,2)
	bit2 = int(b,2)
	g = []
	nobits = len(b)	

	print a.rjust(len(a)+len(b)-1)
	str = "x"+b;
	print str.rjust(len(a)+len(b)-1)

	print "-" * (len(a)+len(b)-1)

	b=reverse(b)
	for i in range (0,nobits):
		if (b[i]=='0'):
			g.append(0)
		else:
			g.append(int((bit1<<i)))
		print bin(g[i])[2:].rjust(len(a)+len(b)-1)
	res=int(g[0])
	for i in range (1,nobits):
		res = int(res) ^ int(g[i])
	print "-" * (len(a)+len(b)-1)
	print bin(res)[2:].zfill(len(a)+len(b)-1)
	print "=" * (len(a)+len(b)-1)


	return res  


print "Binary form:\t",val1,"x",val2
print "Decimal form:\t",int(val1,2),"x",int(val2,2)
print ""
showpoly(val1)

showpoly(val2)

print "\nWorking out:\n"
res=multiply(val1,val2)

print "\nResult: ",res

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 395 views
395 views
Problem : I have the very unique question from the programming background. They want me to write the function which should multiply (a) and (b) but not writing it like below one: multiplyfunc(a,b){ return a*b; } They want me to do a math with below approach multiplyfunc(a)(b) Is it possible to do it?
asked Jan 14, 2020 alecxe 7.5k points
0 votes
1 answer 16 views
16 views
In this code : def __repr__(self):   return '<%s %s (%s:%s) %s>' % (     self.__class__.__name__, self.urlconf_name, self.app_name,     self.namespace, self.regex.pattern) What is the significance/purpose of def__repr__(self) ?
asked Oct 23, 2020 psandprop 2.4k points
1 vote
1 answer 677 views
677 views
Problem: Hello Pal, I have a query regarding the python programming language. The question is, which method will return an empty string when it has attempted to read beyond the end of a file? Do you have any idea about this? Thanks
asked Mar 16, 2020 Gavin 15.3k points
0 votes
1 answer 2 views
2 views
Problem: What is the difference between difference() and symmetric_difference() methods in python sets? a.symmetric_difference(b) are all the elements that are in exactly one set, e.g. the union of a - b and b - a. >>> a.symmetric_difference(b) {2, 3, 4, 5} >>> (a - b).union(b - a) {2, 3, 4, 5}
asked Jan 11 Mashhoodch 5.7k points
1 vote
1 answer 38 views
38 views
Problem: Recently I started learning python and just about to know more about list and tuple in python. I am a little bit known to the list and tuple and I can add or subtracts elements in a list. I wonder how to multiply all elements in a list python? I tried it in the adding process but it didn&rsquo;t work for me. Is it doable? Could anybody please show me the way to do it?
asked Apr 3, 2020 Gavin 15.3k points
0 votes
1 answer 22 views
22 views
Problem: The strftime() method returns a string representing date and time using date, time or datetime object.ou will to convert date, time and datetime objects to its equivalent string (with the help of examples).The datetime object containing current date and time is stored in now variable.
asked Jan 10 Mashhoodch 5.7k points
0 votes
1 answer 20 views
20 views
Problem: This Question / Answer (Python call constructor in a member function) says it is possible to to call the constructor from within a member function. How do I do that? Is it good a style? I tried it with the following code: class SomeClass(object): def __init__( ... = SomeClass(True) def main(): inst = SomeClass(False) inst.build_new() print(inst.field) if __name__ == '__main__': main()
asked Jan 6 Mashhoodch 5.7k points
1 vote
2 answers 328 views
328 views
Problem: I am new in programing and and I am confused about python function names follow the same rules for naming variables. Can anyone explain?
asked Apr 3, 2020 LizzyM 6.1k points
1 vote
1 answer 25 views
25 views
Problem: I am an intermediate level java programmer, and I can write codes in java bit fluently. Recently, I am learning code in python. If anybody could tell me how can I return multiple values in python it would be very helpful to me. I know the ways how to do it in java but not in python. Thanks
asked Mar 10, 2020 Gavin 15.3k points
0 votes
1 answer 162 views
162 views
Problem: I am new in programming and stuck in a query, can anyone guide me? Write an expression that evaluates to True if the str associated with s starts with "p"?
asked Feb 17, 2020 maddi86 5.4k points