简体   繁体   中英

ValueError: invalid literal for int() with base 10 : '1 2 3 4 5'

this is the code just to find some sort of product :print the product of all the number in this array Modulo 10^9+7

         n=int(input())
            answer=1
            b=10**9
            array_1=[]
            for i in range(n):
                array_1.append(int(input()))
            for j in range(n):
                answer=(answer*array_1[j])% (b+7)
            print(answer)

this my code in python 3 working properly in jupyter notebook, but on python (3.5.2) it is showing error for input 5 and then 1 2 3 4 5

        Execution failed.
        ValueError: invalid literal for int() with base 10 : '1 2 3 4 5'

        Stack Trace:


        Traceback (most recent call last):
        File "/hackerearth/PYTHON3_46/s_ad.py3", line 16, in 
        array_1.append(int(input()))
        ValueError: invalid literal for int() with base 10: '1 2 3 4 5'

please some one help me to solve this error as im newbie in python

array_1.append(int(input())) by using this im trying get an array of int by taking value one by one from user input –

But it looks like you are entering the numbers one after the other as a single string with each number separated by a space. In that case, you should use split to get the individual numbers:

array_of_ints = [int(num) for num in input().split()]
array_1 += array_of_ints

If you are trying to input the numbers one by one, your code seems correct. You just need to make sure you actually do enter the numbers one by one. ie 1 then press Enter, 2 then press Enter, and so on...

I'm not completely sure what you are trying to achieve here, but just based on looking at it, your code would not accept any of the inputs after 1 2

If you are running this from a terminal, there should be a new line between each input, ie

./your_program.py
4
4
3
2
1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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