I was faced with a very strange problem. With print() this code works.
def max_pairwise_product(numbers):
max_1 = max(numbers)
numbers.remove(max_1)
max_2 = max(numbers)
return max_1 * max_2
if __name__ == '__main__':
input_n = int(input())
print() # <- comment of this line breaks the code
input_numbers = [int(x) for x in input().split()]
print(max_pairwise_product(input_numbers))
If I comment or delete the 10-th line with print() I got an error:
Traceback (most recent call last):
File "C:\Users\...\maximum_pairwise_product_fast.py", line 12, in <module>
print(max_pairwise_product(input_numbers))
File "C:\Users\...\maximum_pairwise_product_fast.py", line 2, in max_pairwise_product
max_1 = max(numbers)
ValueError: max() arg is an empty sequence*
Process finished with exit code 1
I use Python 3.9. PyCharm. I tried to launch with different virtual environments with Python 3.8 and 3.10 – the same error. When I launch in Jupyter and Colab – it is fane – no error.
There are no issues with any other Python script. I used the installation for several months and there was nothing strange.
It is so strange that I have no idea. Could you please help me?
CodePudding user response:
Try checking if your python interpreter is working correctly and is the latest python It might fix it or there's probably a problem with your ide.
CodePudding user response:
Back in 2018 or so, I found a similar strange issue in a Python program I downloaded that solved Rubik's cubes. Basically, the program ran just fine under Linux, but under Windows it was erroring out at a certain line that looked fine.
I ran "pyflakes" on that program, but pyflakes reported that nothing was wrong. Odd.
The line that supposedly contained the error was a list comprehension, much like your line here:
input_numbers = [int(x) for x in input().split()]
I replaced the list comprehension with a normal for
-loop, and then the code ran fine with no errors. Basically, if I were to rewrite your line, I would replace it with the following three lines:
input_numbers = []
for x in input().split():
input_numbers.append(int(x))
I have no idea why the error was happening in the first place, and only on Windows. (The version of Python3 I was using certainly supported list comprehensions; I checked.) But once I replaced the list comprehensions with a for
-loop, the code worked.
So my advice is to replace your list-comprehension with a for
-loop and see if that solves anything.
I don't know if that will work, but it's worth a shot.
P.S. I'm curious: Which operating system(s) does the error happen on?
P.P.S. I recommend adding input text to your input()
calls (such as input("Type some space-separated numbers: ")
, even if just for posting here. Otherwise, when stackoverflow users run your code, it looks like your code is hanging.
CodePudding user response:
Thank you all for helping to refer me in the right direction. The reason for the error was a bug in PyCharm. It sent not was typed. The issue disappeared after the PyCharm update from version 2022.1.1 to 2022.1.2.