LoginSignup
2
0

More than 3 years have passed since last update.

pythonでナベアツ問題やってみた

Posted at

はじめに

3の倍数と、3のつく数字の時に文字を変える、という構文にチャレンジ。

〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜

s = "Go!!!!"

for i in range(1,41):
  if i % 3 == 0:
    print (s)
  elif '3' in str(i):
     print (s)
   else:
     print (i)
  

〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜

1
2
Go!!!!
4
5
Go!!!!
7
8
Go!!!!
10
11
〜〜〜〜〜(省略)〜〜〜〜〜〜〜〜〜〜〜
Go!!!!
28
29
Go!!!!
Go!!!!
Go!!!!
Go!!!!
Go!!!!
Go!!!!
Go!!!!
Go!!!!
Go!!!!
Go!!!!
40

終わり

これでいけました。簡単です。
後半うざいですね。。笑
inを使って指定した文字列が含まれていたらTrueを返します。

なので今回は変数iをstring型に変換し、
その中の文字列'3'を、文字列を代入した変数sを返す。

こんな感じですかね、ニュアンス違ったらすみません。

やってみたら楽しかった。

2
0
3

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
2
0