user3395662 user3395662 - 1 year ago 57
Python Question

Can someone tell me the difference between these two python codes? I'm having a test in 2 hours, any help is appreciated

Thanks in advance.

a=int(input("Unesi a: "))
b=int(input("Unesi b: "))
c=a+b
print(c)


I do not understand what the difference is because it does the same thing.

d=input("Unesi c: ")
e=input("Unesi d: ")
f=int(d)+int(e)
print(f)

Answer Source

Both code snippets ultimately do the same thing, provided that the input is valid for a b d and e. If the input is not a number, a and b will give you a ValueError right away, since you're evaluating the string input to an int immediately. d and e will accept any sort of input, including those with non numerical data, but you'll get the same ValueError when doing the addition. Same result, different timing when errors occur.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download