Reputation: 77
x1 = 1
x2 = 2
ans=int(input(x1, "+", x2, "="))
if (ans==(x1+x2)):
print("Correct")
else:
print("Incorrect")
I have this code which is supposed to tell the user to input a value for what they think is the correct answer for x1 + x2 , however when I run the code, the input part is giving me an error. Is there something I am doing wrong?
Upvotes: 0
Views: 415
Reputation: 5388
Input takes only 1 parameter. By using comma, you are passing 4. So, just convert them into a single string by using '+' operator instead of ','.
x1 = 1
x2 = 2
ans = int(input(str(x1) + "+" + str(x2) + "="))
# As string concatenation is an expensive operation, the following
# might be an optimal approach for acheiving the same result.
ans = int(input("".join(str(x1), "+", str(x2), "=")))
# But in this scenario, it doesn't matter because the number of strings
# being concatenated and the length of them are both small. So, it won't
# make much difference.
# Also now, Python3.5+ supports `f-strings`. So, you can do something like
ans = int(input(f"{x1} + {x2} = "))
if (ans == (x1 + x2)):
print("Correct")
else:
print("Incorrect")
Upvotes: 2
Reputation: 1569
Replace
ans=int(input(x1, "+", x2, "="))
with
ans = int(input("".join(map(str, [x1, "+", x2, "="]))))
Upvotes: 0