I want to add up the numbers in the row (w, x, y, z are all a part of a row) that the user gives me. I tried doing this:
(w, x, y, z) = input("Enter values: ").split()
row = w + x + y + z
print(row)
but it does not work.
(w, x, y, z) = input("Enter values: ").split()
row = int(w) + int(x) + int(y) + int(z)
print(row)
This code should do the job. You must remember that the input function returns a string which needs to be converted into integer using the int() function.
In your code, it gets concatenated since the numbers are considered as strings.
Cheers!
input returns strings not integer or float value. You need to convert the strings you get to integers.
w, x, y, z = input('Enter values: ').split()
row = int(w) + int(x) + int(y) + int(z)
or you can loop through the digits:
numbers = input('Enter values: ').split()
for integer in numbers:
total += integer
Keep in mind that with this code if the user enters anything other than 4 digits the code will not work. You can fix that by implementing try and except blocks or checks when receiving the input like so:
run = True
While run:
try:
w, x, y, z = input('Enter values: ').split()
row = int(w) + int(x) + int(y) + int(z)
run = False
except Exception as e:
print(e)
print("Please enter 4 digits seperated by a single space")
This will make the code loop until the user follows the instructions.
sum(int(num) for num in input('Enter Values: ').split())