sum up to 4 digits with limitations

There are a few problems with your code, not just the one that you have described:

Problem 1 – Floats as Strings

num = str(num)
num = list(num)

Say you input the number 123, this as a float is 123.0 so when converted to a string and then list you’d have num == ['1', '2', '3', '.', '0']. This is going to cause you problems when you assign w as '.' is not a valid character that int can understand:

num = int(num)
num = str(num)
num = list(num)

or:

num = list(int(str(num)))

Problem 2 – Assigning values

x = (int(num)[0])
y = (int(num)[1])
z = (int(num)[2])
w = (int(num)[3])

This won’t work, when you want to index a value in a list you should use num[i] but you’re calling int on the whole list int(num). The extra brackets are also superfluous:

x = int(num[0])
y = int(num[1])
z = int(num[2])
w = int(num[3])

Problem 3 – Indexing

Great, now you can get values into x, y, z and w. But only if you input 4 digits, if you input any less like 123, you’ll have num == [1,2,3]. This means that when you call w = int(num[3]) you’ll get an IndexError because num doesn’t have any values at index 3. At this point you’ve mentioned that you can’t use the len function or loops, so instead you can use this code snippet (from this answer):

length = sum(map(lambda x:1, num))

Then use if statements to get the appropriate values (I’ve also assigned default values of 0 to each variable):

x, y, z, w = 0, 0, 0, 0
x = int(num[0])
if length >= 2:
    y = int(num[1])
if length >= 3:
    z = int(num[2])
if length >= 4:
    w = int(num[3])

At this point x, y, z and w all have values and you can just use:

my_sum = x + y + z + w

to get your final value:

num = float(input("Please enter any number up to 4 digits: \n"))

if num < 0:
    print ("Please enter a positive number")
elif num > 9999:
    print ("Please enter up to 4 digits")
elif (num % 1 != 0): 
    print ("Please enter only integer and not float")

num = list(str(int(num)))
length = sum(map(lambda x:1, num))

x, y, z, w = 0, 0, 0, 0
x = int(num[0])
if length >= 2:
    y = int(num[1])
if length >= 3:
    z = int(num[2])
if length >= 4:
    w = int(num[3])

my_sum = x + y + z + w

Bonus Problem – Input Validation

This only leaves the problem of having to validate user input which you can do using recursion, this was my final solution that I put together quickly:

def get_input(msg):
    try:
        num = float(input(msg))
    except ValueError:
        print("Please enter a number, not a string")
        return get_input(msg)
    if num < 0:
        print ("Please enter a positive number")
        return get_input(msg)
    elif num > 9999:
        print ("Please enter up to 4 digits")
        return get_input(msg)
    elif (num % 1 != 0): 
        print ("Please enter only integer and not float")
        return get_input(msg)
    else:
        return int(num)

num = get_input("Please enter any number up to 4 digits: \n")
s = sum(map(int,list(str(num))))
print(s)

Browse More Popular Posts

Leave a Comment