0

I'm a beginner programmer. I want to write a program that gives me the maximum product of all the products of 4-adjacent digits in an input number. So, if the input is "12345678" Possible selections are 1234, 2345,3456,4567,5678 and the largest product is 5*6*7*8.

My code:

number = str(input("Enter a number:"))
i = 0
L = []
while (i!=len(number)-3):
    a = int(number[i])
    b = int(number[i+1])
    c = int(number[i+2])
    d = int(number[i+3])
    product = a*b*c*d
    L.append(product)
    i = i+1
print(L)
print(number)
print(max(L))

I need to apply this to a 1000-digited number. My code works for 8-digited input number and gave an answer for a 500-digited number.

But I tried it with a 600-digited number and it throws this error.

I understand ValueError is an error that appears when the argument given to a function call has correct type, but inappropriate value. There are also examples of when the user gives a string "Alexander" as input in code Eg: int(input("Enter a number"))

the error is for '' an empty string that cannot be converted to an integer. But I cannot understand where/why the empty string was formed.

I have read a few other answers of this Error type, but all involve code that use features of Python I am NOT familiar with and hence cannot understand. I'm just a beginner! So, please help!

And apologies for breaking any rules laid out with regards to question formation!

3
  • can you post the full stacktrace? Commented Nov 2, 2015 at 14:44
  • this works to return a list from the 600-digit number when I test it. You may want to validate your input string per @Randy C's answer below. Commented Nov 2, 2015 at 14:50
  • try putting your number in a file and open the file directly in python rather than reading it via input (it might be a problem with shell redirection) Commented Nov 2, 2015 at 14:54

2 Answers 2

1

You've got a space there, not an empty string. Most likely, you just hit the space bar at the end of your input, and Python can't convert that to an integer. You can either just ensure that you don't leave a space at the end, or do some checking of your input (e.g., add a line number = number.strip() to remove any trailing whitespace).

Sign up to request clarification or add additional context in comments.

2 Comments

No, I made sure to leave no empty spaces at the end of the number.
White spaces crept up in the middle of the string in the process of copy-pasting! I should have manually checked. Thank you for your help! :)
0

Validate your input as numeric, and strip any whitespace:

number ='123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'

def foo(number):
    number = number.strip()
    if number.isdigit():
        i = 0
        L = []
        while (i in range(len(number)-3)):
            a = int(number[i])
            b = int(number[i+1])
            c = int(number[i+2])
            d = int(number[i+3])
            product = a*b*c*d
            L.append(product)
            i = i+1
        return max(L)

This functions should return a None if user has provided invalid input (e.g., "Alexander"), this should avoid the error you describe:

There are also examples of when the user gives a string "Alexander" as input in code Eg: int(input("Enter a number"))

You can also simplify this using a generator statement for a set of only the unique results:

def foo2(number):
    number = number.strip()
    if number.isdigit():
        return max({int(number[i]) * int(number[i+1]) * int(number[i+2]) * int(number[i+3]) for i in range(len(number)-3)})

1 Comment

White spaces crept up in the process of copy pasting the huge number from the site to the input! Thank you for your help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.