0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python学習ノート_003

Last updated at Posted at 2019-12-15

本の第一章のサンプルコードを理解した上で練習のために下記のように修正しました。

  • ポイント:
  • バックスラッシュ(\)を使う(Macには「option + ¥」で入力する)
  • 文字列中にバックスラッシュを使うと改行後のスペースもそのまま出力される
  • 2つ改行した文字列の間にバックスラッシュを使うとスペースなしで連結できる
sample_01.py
import random

# リストを定義する
subjects = ['\
','あな\
            たは']
verbs = ['好き'\
         'です',
         '嫌い'\
         'です']
nouns = ['夏が','秋が']

# リストから1つ要素を選ぶ
subject = random.choice(subjects)
verb = random.choice(verbs)
noun = random.choice(nouns)

# 単語を連結してフレーズを作る
phrase = subject + ' ' + noun + ' ' + verb

# フレーズを出力する
print(phrase)

# 出力結果の1つ
# あな            たは 秋が 好きです
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?