while 条件式:
処理
カウンタ変数の更新
単純なようで、使わないとすぐわすれちゃいますね
入門からプログラミングを勉強して for i in range () の形を覚えちゃうとすっごく便利なのでwhileは全然使わず、記憶の彼方に消えてしまいました。
使い方は
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 0 | |
while n <= 2: | |
print (a) | |
n += 1 |
という感じです
PaizaのスキルチェックはDの問題を相変わらず解いていますが、その中で while は一回も使いませんでした
今一度 while の使い方について考えてみましょう
- while文は初期値とカウンタ変数の更新が楽
- for文と違い、回数に規定がない
もちろんfor文もrangeの部分をうまく変えてしまえば同じこともできると思いますが、基本的にはこの2点だと思います
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
tozan = 0 | |
while tozan < 3776: | |
climb = random.randint(10,500) | |
print("富士山を"+ str(climb) + "m登った") | |
tozan += climb | |
print ("富士山登頂!") |
whileもちゃんと身に着けて、使うべきときに使う判断ができるようになりたいですね
0 件のコメント:
コメントを投稿