简体   繁体   English

第8行:ValueError:int()以10为底的无效文字:“'

[英]Line 8: ValueError: invalid literal for int() with base 10: ' '

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. 我想编写一个程序,为我提供输入数字中所有4位相邻数字产品的最大乘积。 So, if the input is "12345678" Possible selections are 1234, 2345,3456,4567,5678 and the largest product is 5*6*7*8. 因此,如果输入为“ 12345678”,则可能的选择为1234、2345、3456、4567、5678,最大乘积为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. 我需要将此数字应用于1000位数字。 My code works for 8-digited input number and gave an answer for a 500-digited number. 我的代码适用于8位输入数字,并给出了500位数字的答案。

But I tried it with a 600-digited number and it throws this error. 但是我尝试用一​​个600位数的数字进行尝试,并抛出了此错误。

I understand ValueError is an error that appears when the argument given to a function call has correct type, but inappropriate value. 我知道ValueError是一个错误,当给函数调用的参数具有正确的类型但值不正确时会出现。 There are also examples of when the user gives a string "Alexander" as input in code Eg: int(input("Enter a number")) 还有一些示例,说明用户何时在代码Eg中输入字符串“ Alexander”作为输入: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. 我已经阅读了有关此错误类型的其他一些答案,但是都涉及使用我不熟悉的Python功能的代码,因此无法理解。 I'm just a beginner! 我只是一个初学者! So, please help! 所以,请帮忙!

And apologies for breaking any rules laid out with regards to question formation! 对于违反有关问题形成的任何规则表示歉意!

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. 最有可能的是,您只需在输入末尾敲击空格键,Python无法将其转换为整数。 You can either just ensure that you don't leave a space at the end, or do some checking of your input (eg, add a line number = number.strip() to remove any trailing whitespace). 您可以只是确保在结尾处不留空格,也可以对输入进行一些检查(例如,添加行number = number.strip()以删除任何尾随空格)。

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 (eg, "Alexander"), this should avoid the error you describe: 如果用户提供了无效的输入(例如“ Alexander”),则此函数应返回None ,这应该避免您描述的错误:

There are also examples of when the user gives a string "Alexander" as input in code Eg: int(input("Enter a number")) 还有一些示例,说明用户何时在代码Eg中输入字符串“ Alexander”作为输入:int(input(“ Enter a number”))

You can also simplify this using a generator statement for a set of only the unique results: 您还可以使用generator语句仅对一set唯一结果进行简化:

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)})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM