Show-Me-the-Code / python

Show Me the Code Python version.
https://github.com/Show-Me-the-Code/show-me-the-code
MIT License
3.91k stars 2.74k forks source link

Code to add two numbers #416

Open Gagaaaa opened 2 years ago

Gagaaaa commented 2 years ago

Please is this the right python code to add two numbers

x = input("Type a number: ") y = input("Type another number: ")

sum = int(x) + int(y)

print("The sum is: ", sum)

SohamRatnaparkhi commented 2 years ago

Yes!

Aakash-2904 commented 2 years ago

it is better to use int while taking the input itself dude

x =int( input("Type a number: ")) y = int(input("Type another number: "))

sum = (x) + (y)

print("The sum is: ", sum) ------------------------------or---------------------------------

x =int( input("Type a number: ")) y = int(input("Type another number: "))

print("The sum is: ", sum(x+y))

hope it helps

gvadakumchery commented 2 years ago

Notice I put int in input this will force the input to be an integer and nothing else! x = int(input("Type a number: ")) y = int(input("Type another number: ")) sum = x + y print("The sum is: ", sum)

shelar1423 commented 2 years ago

yes

rahul188 commented 2 years ago

what if the user add non-numerical like abc

Aakash-2904 commented 2 years ago

If u don't declare the data type ...and u give two strings "ABC" & "DEF"...when u add both these strings u get ABCDEF Hope this helps :) GIT

linyongqi3 commented 2 years ago

this is right

ishan3199 commented 2 years ago

Agree with @Aakash-2904 Always force the input to integer for summing them up

haitao888 commented 2 years ago

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

MarkTina commented 2 years ago

OK. your email has been received.

AK16092003 commented 2 years ago

get_input function documentation

s         type: str
datatype  type: class

** Accepts input in specific datatype with displaying the string **
** If the user enters a value of the wrong datatype,
       then the function calls itself again until the user gives the required data type **

Example:

>>> a = get_input("Enter : ",int)
Enter : 12
>>> b = get_input("Enter :",int)
Enter :12.3

Error in user input
Please Enter Again with the proper DataType!

Enter :12
>>>

Function Code:

def get_input(s="" , datatype = int):

    user_input = input(s)
    try:
        num = datatype(user_input)
    except:
        print("\nError in user input")
        print("Please Enter Again with proper DataType!\n")
        num = get_input(s , datatype)
    return num

Program Code to Add 2 numbers:

a = get_input("Enter value a = " , int)
b = get_input("Enter value b = " , int)
ans = a+b
print("Answer = ",ans)
AK16092003 commented 2 years ago

the above code is written to make user input validation simpler in python for any datatype Hope this helps! Thank you!

JIMLIARY commented 2 years ago

unsubscribe

------------------ 原始邮件 ------------------ 发件人: "Show-Me-the-Code/python" @.>; 发送时间: 2022年4月26日(星期二) 凌晨0:08 @.>; @.***>; 主题: Re: [Show-Me-the-Code/python] Code to add two numbers (Issue #416)

the above code is written to make user input validation simpler in python for any datatype Hope this helps! Thank you!

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

haitao888 commented 2 years ago

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

sayampradhan commented 2 years ago

Please is this the right python code to add two numbers

x = input("Type a number: ") y = input("Type another number: ")

sum = int(x) + int(y)

print("The sum is: ", sum)

Hello @Gagaaaa, Your code is correct. However, the input variable will also take strings as input which may cause errors while adding them. The safe side could be to take integers as input, instead of typecasting them later. The code can be corrected by just typecasting the input you are taking from the user as well as by choosing variable names, for eg. number_1, num1, num_1, etc.

Corrected Code

x = int(input("Type a number: "))
y = int(input("Type another number: "))

sum = x + y

print("The sum is: ", sum)
JanitaRiyaz commented 1 year ago

x = input("Type a number: ") y = input("Type another number: ")

sum = int(x) + int(y)

f string can use to concatenate string with int or plugin the value. print(f"The sum is: {sum}")

SachCoder commented 1 year ago

x = input("enter first no: ") y= input("enter second no: ") print("sum of two number is : " , x+y)

Mojitaba34 commented 1 year ago

Yes but I suggest that you could use lambda here take a look at this

`x = int(input("Enter the first number: ")) y = int(input("Enter the second number: "))

input_sum = lambda x, y: x + y

print(f"The sum of the input numbers is -> {input_sum(x, y)}") ` This gives the advantage of using this function anywhere you need

dreamer561 commented 1 year ago

num1=int(input("Enter first num")) num2=int(input("Enter Seccond num")) sum=num1+num2 OR with function

kinderasteroid commented 1 year ago

Your Code is 100% Fine, But Make Sure that You take the input Directly In Format of

x = int(input("Type a number: ")) y = int(input("Type another number: ")) sum = x + y print("The sum is: ", sum)