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:

1 Like

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

Dear Mr. Parrott,

Thank you very much for your support. As you suggested, after commenting out the non-code lines and saving the script with UTF-8 encoding, it worked perfectly.

I appreciate your helpful advice!

Best regards,

Hodai

1 Like

Certainly! Here’s the English translation of your heartfelt message:


I’m incredibly grateful for your support. Although I asked everyone about this problem, I’m an absolute beginner when it comes to Python. So, I thought I’d try to learn what I wanted to do using AI and asked for your help. However, I felt like I wouldn’t be able to achieve what I intended, and I had given up and stepped away from Python. But after rereading your support, I realized that there are people out there who offer such extensive help, and it made me want to try again. Even though I’m elderly, I believe that if I still have the will to challenge myself, there’s still something I can do, so I’ve decided to give it another shot. It’s thanks to all of you. Thank you very much.

1 Like