Addition function in Python is not working as expected

2024/11/15 23:54:44

I tried to create a function using which I want to do mathematical operations like (Addition and Multiplication), I could able to prompt the values and when I insert the values result is not returning as expect. enter image description here

Code

def addition(num1,num2):    return num1+num2 def
multiplication(num1,num2):return num1*num2print("1.addition") 
print("2.multiplication") 
choice  = int(input("Enter Choice 1/2"))num1 = float(input("Enter First Number:")) 
num2 = float(input("Enter Second Number:")) 
sum = float(num1)+float(num2) 
multiply = float(num1)*float(num2) 
if choice == 1:     print("additon of {0} and {1} is".format(num1,num2, sum)) 
elif choice == 2:   print("additon of {0} and {1} is".format(num1,num2, multiply))
Answer

You don't need the functions nor the sum or multiply variables, just put the operation in str.format(), and you were missing the last position.

# def addition(num1,num2):  
#     return num1+num2
# def multiplication(num1,num2):
#     return num1*num2print("1.addition")
print("2.multiplication") 
choice = int(input("Enter Choice 1/2"))num1 = float(input("Enter First Number:"))
num2 = float(input("Enter Second Number:"))
# sum = float(num1)+float(num2)
# multiply = float(num1)*float(num2) 
if choice == 1: print("additon of {0} and {1} is {2}".format(num1,num2, num1 + num2))
elif choice == 2:print("additon of {0} and {1} is {2}".format(num1, num2, num1 * num2))

And know that you can use fstrings (>= Python 3.6):

if choice == 1:print(f"additon of {num1} and {num2} is {num1 + num2}")
elif choice == 2:print(f"additon of {num1} and {num2} is {num1 * num2}")

Old-style formatting:

if choice == 1:print("additon of %s and %s is %s" % (num1,num2, num1 + num2))
elif choice == 2:print("additon of %s and %s is %s" % (num1, num2, num1 * num2))

Or string concatenation:

if choice == 1:print("additon of " + num1 + " and " + num2 + " is " + (num1 + num2))
elif choice == 2:print("additon of " + num1 + " and " + num2 + " is " + (num1 * num2))

Each person does it a different way and it's useful to know all of them sometimes.

https://en.xdnf.cn/q/120399.html

Related Q&A

Probability Distribution Function Python

I have a set of raw data and I have to identify the distribution of that data. What is the easiest way to plot a probability distribution function? I have tried fitting it in normal distribution. But …

how to convert a np array of lists to a np array

latest updated: >>> a = np.array(["0,1", "2,3", "4,5"]) >>> a array([0,1, 2,3, 4,5], dtype=|S3) >>> b = np.core.defchararray.split(a, sep=,) >…

Regex stemmer code explanation

Can someone please explain what does this code do?def stemmer(word):[(stem,end)] = re.findall(^(.*ss|.*?)(s)?$,word)return stem

Scraping data from a dynamic web database with Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

Python representation of floating point numbers [duplicate]

This question already has answers here:Floating Point Limitations [duplicate](3 answers)Closed 10 years ago.I spent an hour today trying to figure out whyreturn abs(val-desired) <= 0.1was occasional…

How to grep only duplicate key:value pair in python dictionary?

I have following python dictionary.a={name:test,age:26,place:world,name:test1}How to grep only duplicate key:value pair from the above?Output should be: "name: test and name:test1"

IndentationError - expected an indented block [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 7 months ago.I get the IndentationError: expected an indented block. I was tryi…

No axis named 1 for object type class pandas.core.frame.DataFrame

I created a DataFrame and I am trying to sort it based on the columns. I used the below code.frame.sort_index(axis=1)But this is causing the below errors------------------------------------------------…

str.replace with a variable

This is probably a simple fix, but having a little trouble getting my head around it; Im reading lines from a different script, and want to replace a line with a variable, however it replaces it with b…

How to generate DTD from XML?

Can a DTD be generated from an XML file using Python?