Open Gagaaaa opened 2 years ago
Yes!
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))
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)
yes
what if the user add non-numerical like abc
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 :)
this is right
Agree with @Aakash-2904 Always force the input to integer for summing them up
这是来自QQ邮箱的假期自动回复邮件。 您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
OK. your email has been received.
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)
the above code is written to make user input validation simpler in python for any datatype Hope this helps! Thank you!
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: @.***>
这是来自QQ邮箱的假期自动回复邮件。 您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
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.
x = int(input("Type a number: "))
y = int(input("Type another number: "))
sum = x + y
print("The sum is: ", sum)
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}")
x = input("enter first no: ") y= input("enter second no: ") print("sum of two number is : " , x+y)
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
num1=int(input("Enter first num")) num2=int(input("Enter Seccond num")) sum=num1+num2 OR with function
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)
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)