Python Syntax Error and questions that result in unwanted output.

Asked 2 years ago, Updated 2 years ago, 13 views

Hello, I am currently writing the code for the anonymous site problem on notepad and running it on powershell, and I am writing it because there are some parts I am not familiar with...

from sys import stdin
n,m=map(int,stdin.readline().split())
n_list=set([stdin.readline() for i in range(n)])
m_list=set([stdin.readline() for i in range(m)])

result=sorted(list(n_list & m_list))

print(len(result))

for i in result:
    print(i)

The desired output value for this is three lines, and the first line is the total number of results
It then outputs duplicate values for each line.
2 ac should be output, but this is how to fix the line gap between a and c.

from sys import stdin
string=list(str(stdin.readline()))
print(string)

The value I put in is ABCDE The output values are ['A', 'B', 'C', 'D', 'E', '\n']. It automatically outputs even though \n is not inserted.

I use an existing file called test.py, and if I write the same code as question 2,

** File "test.py", line 4
    print(string);
    SyntaxError: invalid syntax**

This error kept failing, so when I created and executed test1.py, the output was normal except for the problem shown in question 2.
Obviously, I want to know why SyntaxError works fine on test.py when I write the same thing.

python

2022-09-20 20:50

1 Answers

A1.

a = [1,2,3,4,5]
for i in a:
    print(i, end='')
#result : 12345
#https://careerkarma.com/blog/python-print-without-new-line/

A2. To finish stdin, you have to enter, which is \n.

from sys import stdin
string=list(str(stdin.readline()).rstrip('\n'))
print(string)

Why don't you remove it before you move on to the list in the str step like this

A3. In Python; how to write is a=3; a*a It is used when writing line scripts together. There seems to be no error in this line, so why don't you check for other code errors? I don't know if it's not in parentheses. Things like this.


2022-09-20 20:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.