質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

while

Whileは多くの言語で使われるコントロール構造であり、特定の条件が満たされる限り一連の命令を繰り返し実行します。

Q&A

解決済

3回答

216閲覧

ループが働かなくなってしまった

退会済みユーザー

退会済みユーザー

総合スコア0

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

while

Whileは多くの言語で使われるコントロール構造であり、特定の条件が満たされる限り一連の命令を繰り返し実行します。

0グッド

0クリップ

投稿2018/11/10 16:25

編集2018/11/10 16:28

前提・実現したいこと

以前に質問をさせていただいたプログラムに再度問題が発生いたしました。どうしたらうまくいくのか、ネットで調べて試行錯誤しましたが、全く効果(?)がありませんでした。なぜうまくいかないのか、皆様のご意見をお聞きしたいです。問題点はコードの下に記してあります。

該当のソースコード

python

1flag = True 2while flag == True: 3 print(' MENU') #メニュー表示 4 print('==========') 5 print('S to start') 6 print('H for help') 7 print('Q to quit') 8 print('==========') 9 print(' ') 10 choice = input('Option?: ') 11 12 if choice == 'H':   #このプログラムの説明 13 print('This program will give you the area of a rectangle.') 14 print('Please provide the height and width when asked.') 15 print('You can quit this program from the main menu by pressing Q') 16 elif choice == 'Q':     #プログラムを終了(Quit) 17 flag = False 18 elif choice == 'S': #プログラムの開始(Start) 19 choice2 = input("please choose the number: ") 20 choice3 = int(choice2) 21 def menuList(choice3): 22 if choice3 == 0: 23 print("bad input") 24 elif choice3 == 1: #円を描く 25 import turtle 26 t=turtle.Turtle() 27 turtle._Screen() 28 elif choice3 == 2:   #四角を描く 29 import turtle 30 smart = turtle.Turtle() 31 for i in range(4): 32 smart.forward(50) 33 smart.right(90) 34 turtle._Screen() 35 elif choice3 == 3: #三角形を描く 36 import turtle 37 board = turtle.Turtle() 38 board.forward(100) 39 board.left(120) 40 board.forward(100) 41 board.left(120) 42 board.forward(100) 43 turtle._Screen() 44 elif choice3 == 4:   #プログラムの終了 45 print("Good bye") 46 flag = False 47 menuList(choice3) 48 else: 49 print('Not a valid choice. Only S, Q and H are valid choices') 50 51print('Program ending.')

4を選択するとプログラムを終了するようにしたいのですが、なぜかループしてしまいます。
なぜ、"flag = False"が働いていないのでしょうか。
もし、解決案が分かる方がいらっしゃいましたら、ご指摘いただきたいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

全体を書き直してみました。

python3

1import turtle 2 3def show_menu(): 4 print(' MENU') #メニュー表示 5 print('==========') 6 print('S to start') 7 print('H for help') 8 print('Q to quit') 9 print('==========') 10 print(' ') 11 12def show_help(): 13 print('This program will give you the area of a rectangle.') 14 print('Please provide the height and width when asked.') 15 print('You can quit this program from the main menu by pressing Q') 16 17def play(): 18 t = turtle.Turtle() 19 while True: 20 choice = input("please choose the number (1:circle,2:rect, 3:trianble:4:bye):") 21 t.reset() 22 if choice == "1": # 円を描く 23 t.circle(120, 360) 24 elif choice == "2": # 四角を描く 25 for i in range(4): 26 t.forward(50) 27 t.right(90) 28 elif choice == "3": #三角形を描く 29 for i in range(4): 30 t.forward(100) 31 t.left(120) 32 elif choice == "4": # プログラムの終了 33 print("Good bye") 34 break 35 else: 36 print("bad input") 37 38print('Program starting.') 39while True: 40 show_menu() 41 choice = input('Option?: ') 42 if choice == 'H': # このプログラムの説明 43 show_help() 44 elif choice == 'Q': #プログラムを終了(Quit) 45 break 46 elif choice == 'S': #プログラムの開始(Start) 47 play() 48 else: 49 print('Not a valid choice. Only S, Q and H are valid choices') 50print('Program ending.')

flag は使わなくしました。
While True は2つあります。それぞれが break することは明確になっています。

投稿2018/11/13 19:09

katoy

総合スコア22324

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

menuList関数内のflag変数は、あくまで関数内でしか利用されず、外側のflagには何の影響も与えず更新もされません。
よって処理は続行されてしまいます。
以下のようにmenuList関数の結果を受けて終了可否判断するのがよいでしょう。

Python

1 # 略 2 def menuList(choice3): 3 if choice3 == 0: 4 print("bad input") 5 # 略 6 elif choice3 == 4: #プログラムの終了 7 print("Good bye") 8 return False # 続行しない。終了する 9 return True 10 flag = menuList(choice3) # 続行可否を受ける 11 # 略

投稿2018/11/10 16:36

編集2018/11/10 16:39
can110

総合スコア38233

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

関数menuListを独立させる意味は一切無いので、取り除いてください。

Python

1if choice == 'H': 2 ... 3elif choice == 'Q': 4 ... 5elif choice == 'S': 6 choice2 = input("please choose the number: ") 7 choice3 = int(choice2) 8 if choice3 == 0: 9 print("bad input") 10 elif choice3 == 1: #円を描く 11 import turtle 12 t=turtle.Turtle() 13 turtle._Screen() 14 elif choice3 == 2:   #四角を描く 15 import turtle 16 smart = turtle.Turtle() 17 for i in range(4): 18 smart.forward(50) 19 smart.right(90) 20 turtle._Screen() 21 elif choice3 == 3: #三角形を描く 22 import turtle 23 board = turtle.Turtle() 24 board.forward(100) 25 board.left(120) 26 board.forward(100) 27 board.left(120) 28 board.forward(100) 29 turtle._Screen() 30 elif choice3 == 4:   #プログラムの終了 31 print("Good bye") 32 flag = False

トップレベル以外で関数を定義するのは、わりあい高度なテクです。
ですので、その利点や使い方が明確に分からないうちは避けた方が良いです。

また、今回の場合そもそもフラグなんて必要なく、
外部のループを無限ループにして適宜breakすれば良いだけの話です。


なんというか、継ぎ接ぎな印象のコードです。
一度書籍やチュートリアルなどで体系的に学ぶことをお勧めします。

バグそのものの原因

関数内のローカル変数とモジュールレベルの変数が別物になっているからです。

Python

1>>> a = 0 2>>> def func(): 3... a = 42 4... 5>>> func() 6>>> print(a) 70

globalというキーワードもありますが、今はまだ手を出すべきでは無いでしょう。

投稿2018/11/10 16:31

編集2018/11/10 16:36
LouiS0616

総合スコア35658

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問