-1

I am quite new to Python3 and I have a hard time coding some stuff using python3.

I want to store the string characters into new variables which I read the new one using the readline() function in python3.

Below is my code with the input file description.

import sys
sys.stdin = open('../data/lecture.txt')
rl = lambda : (sys.stdin.readline().strip())

for _ in range(int(rl())):{
    print(rl())
}

console

abbaaccb
dddcccbbbaaa
geegeegeegeebabybabybaby
oh

As you can see, I opened the lecture.txt and printed the values inside it using rl(). When I printed it, it showed the string characters as shown above.

However, when I specified the new variable and stored the characters into it, it throw error indicating syntax error as below

import sys
sys.stdin = open('../data/lecture.txt')
rl = lambda : (sys.stdin.readline().strip())

for _ in range(int(rl())):{
    new_variable = str(rl().strip())   //where error occurred
}

I tried to google to resolve my issue but failed.

I attached the input file for your kindly help.

lecture.txt

4
abbaaccb
dddcccbbbaaa
geegeegeegeebabybabybaby
oh

Thanks

sclee1
  • 1,095
  • 1
  • 15
  • 36
  • Possible duplicate of [How do I read in a text file in python 3.3.3 and store it in a variable?](https://stackoverflow.com/questions/21540006/how-do-i-read-in-a-text-file-in-python-3-3-3-and-store-it-in-a-variable) – Mel Jul 10 '18 at 08:24
  • 1
    Python doesn't use curly braces for code blocks. Remove them and it'll work. Currently your code is a loop whose body is a set literal, and assignments aren't allowed in literals. – Aran-Fey Jul 10 '18 at 08:25
  • 2
    I don't understand how someone can know about things like lambda expressions but not that Python doesn't use braces. – Daniel Roseman Jul 10 '18 at 08:32
  • @DanielRoseman I'm assuming they found some joke code someone wrote, didn't realize it was joke code, and tried to figure out how to make it do what they wanted. – abarnert Jul 10 '18 at 08:33
  • @DanielRoseman: inter-web cut 'n paste. – cdarke Jul 10 '18 at 08:34
  • When you're getting an error, never. **ever** post a question without including it. – jpmc26 Jul 10 '18 at 09:29

1 Answers1

2

The reason for the SyntaxError is that Python doesn't use braces for blocks, it uses indentation.

Your original code sort of works, but in a very misleading way:

for _ in range(int(rl())):{
    print(rl())
}

This looks like a compound block statement, but it's actually a single-line compound statement, where the statement controlled by the for is a set display being used as an expression statement, and of course the useless set {None} isn't there for any reason, it's just being evaluated for the side effect of calling print.

Your second version is trying to put an assignment statement inside a set display, which is illegal—statements can never go inside any kind of expression in Python.

If this is code you found somewhere, it was written as a joke, not as a foundation for you to build on or learn from. Almost everything it does is ridiculous:

  • You don't need to, or want to, reassign sys.stdin just to open a file.
  • You don't need to define a function just to read integers.
  • If you do want to define a named function in a statement, use def, not lambda.
  • There's no good reason to call strip() on a string you already stripped.
  • You don't need to strip off the whitespace anyway to call int; int('2\n') is already 2.
  • You don't need to call str on something that's already a string.
  • Obviously, you shouldn't wrap function calls with side effects inside a set display that creates a set you never use.

So, just throw it away and start over. Here's a more idiomatic way to do what that code is doing:

with open('../data/lecture.txt') as f:
    count = int(f.readline())
    for _ in range(count):
        new_variable = f.readline().strip()
abarnert
  • 354,177
  • 51
  • 601
  • 671