-1

I was hoping on some input concerning the use of 'user inputted' arguments as variable value amounts to be used in a calculation within a function... this will no doubt be a very simple issue but I am new to python and so far my attempts at fixing it have been flawed. The function is a simple calculation of adding two numbers together solely for the purpose of introducing new students to functions. I have no issues with the function working as intended when I feed hard coded values as integers into the parameters (which are included in this example code) but when I try to pass 'user input' argument values in when actually running the program the function simply concatenates the two values rather than adding them, this is (I presume) because the input values are in the format of a 'string'.

I have tried introducing 'int' statements after the 'input' statements within the argument variable code but this results in an error 'invalid literal for int()' I have also tried this at different points of the function code itself with no luck..... so how would I go about making sure the values are recognised as integers before or during calculation? Thanks

def getSum(num1, num2):   
    calc = num1 + num2 
    return calc    

num1 = input("Type in your first number to add: ")
num2 = input("Type in your second number to add: ")


result1 = getSum(num1, num2)




answer = getSum(10, 5)
answer2 = getSum(155, 56668) 



print(answer) 
print(answer2)
print(result1)
L Moon
  • 25
  • 5
  • 1
    "*I have tried introducing 'int' statements after the 'input' statements*" I do not see these `int` statements, please show them. – Willem Van Onsem Oct 23 '17 at 19:01
  • 1
    why exactly does int(input("Type...")) not work for you? what is the problem? do you get an exception? – inxoy Oct 23 '17 at 19:02
  • If you follow what @inxoy said, I can only see that you'd get an error if the user inputs some non-integers – Tom Burrows Oct 23 '17 at 19:09
  • Looking at your comments it could be a case of misformatting on my part..... the 'int' statements in my code were typed AFTER the 'input' statements and not before so I think this maybe where iv gone wrong – L Moon Oct 23 '17 at 19:10
  • int() should work correctly if the user only enters integer values - otherwise you have to wrap it with a try and catch block :D – inxoy Oct 23 '17 at 19:14
  • @LMoon if I could help you, I would be pleased if you could accept my answer below by clicking onto the tick. if not where do u need further help? – inxoy Oct 23 '17 at 19:16
  • Just ran the code successfully after changing them around...yeh you was right inxoy... a simple case of mixed order of statements on my part.... thanks for the info ;) – L Moon Oct 23 '17 at 19:18

2 Answers2

0

int() should work correctly if the user only enters integer values - otherwise you have to wrap it with a try and catch block

try:
    num1 = int(input("Type in your first number to add: "))
    num2 = int(input("Type in your second number to add: "))
catch Exception as ex:
    pass //do nothing
inxoy
  • 3,484
  • 2
  • 13
  • 21
  • I did this and the code worked :) although I didn't include the bottom part (everything past 'catch Exception') but it still worked as I needed it to just by swapping the 'int' and 'input' statements around...... thanks inxoy – L Moon Oct 23 '17 at 19:25
0

Just introduce int() for num1 and num2 in line2. So the new code will be:

def getSum(num1, num2):   
    calc = int(num1) + int(num2) 
    return calc    

num1 = input("Type in your first number to add: ")
num2 = input("Type in your second number to add: ")


result1 = getSum(num1, num2)




answer = getSum(10, 5)
answer2 = getSum(155, 56668) 



print(answer) 
print(answer2)
print(result1)
Mukul Sharma
  • 141
  • 1
  • 2
  • 9