What is the cause of the SyntaxError?

Hello. Nice to meet you. I am Japanese.
I am not very good at English, so I may not be able to explain things well.
Even so, I am studying because I want to learn about Python.
The reason is that I want to use it for writing senryu, which is my hobby.
I asked ChatGPT to create a program for me,
but when I pasted it into Python and ran it, it gave me an error and did not work.
Here is the program.
Please tell me what is wrong and what I should do.
Thank you very much.

import random

単語リスト(例)

words_5 = [“空”, “花”, “風”, “夢”, “光”]
words_7 = [“旅の途中”, “春の訪れ”, “星降る夜”, “海の声”, “街の灯り”]

ランダムに単語を選ぶ

line1 = random.choice(words_5)
line2 = random.choice(words_7)
line3 = random.choice(words_5)

川柳を作成

senryu = f"{line1}\n{line2}\n{line3}"
print(senryu)
SyntaxError: multiple statements found while compiling a single statement

Hello,

I tested your script. It is working just fine. Maybe it is other lines of code that are active on your terminal that are causing the error? Generally, when an exception is generated, it points to the line that is causing the error.

This is the script that I tested:

import random

words_5 = ['空', '風', '夢', '光']
words_7 = ['旅の途中', '春の訪れ', '星降る夜', '海の声', '街の灯り']

line1 = random.choice(words_5)
line2 = random.choice(words_7)
line3 = random.choice(words_5)

senryu = f"{line1}\n{line2}\n{line3}"
print(senryu)

For a particular test, the output was:

空
街の灯り
空

By the way, in order to make the script like this, use the following instructions when posting your script:

I’m not sure why that error occurred. but your program worked for me after I commented out the non-code lines:

import random

# 単語リスト(例)
words_5 = ["空", "花", "風", "夢", "光"]
words_7 = ["旅の途中", "春の訪れ", "星降る夜", "海の声", "街の灯り"]

# ランダムに単語を選ぶ
line1 = random.choice(words_5)
line2 = random.choice(words_7)
line3 = random.choice(words_5)

# 川柳を作成
senryu = f"{line1}\n{line2}\n{line3}"
print(senryu)

Be sure to save it as UTF-8. Otherwise specify a non-default source encoding PEP 263 – Defining Python Source Code Encodings | peps.python.org in the first two lines.

Thank you for your prompt response.
It was very helpful.
I would like to try based on the materials you provided.
I am very happy about this.

1 Like