2016-09-22 7 views
0

私はwhileループを実行して、サイコロの数とランダムなサイコロをする側の数を入力するようにPythonに問題があります。 2番目のループと追加のループで、私は彼らが続行したいかどうか質問したい。 'n'または 'no'を入力すると、プログラムは終了します。フローを制御するローカルブールを設定する

私はグローバル変数を使ってこのロジックを動作させることができました。初めて変数を呼び出すと関数内でその変数を変更することができました。このようなグローバル変数は、非常にPythonのやり方ではありません。私はこれを改善したいと思います。

次のコードは、ユーザーに終了を求めることがないという点を除いて機能します。これは変数がwhileループの始めにTrueに設定され続けるためですが、グローバル変数に頼らずにフラグを設定する方法はわかりません。

True/False変数をローカル(非グローバル)に設定し、それをプログラムのフローを制御するために使用するにはどうすればよいですか?

import sys 
import random 

def get_user_input(first_loop): 
    if not first_loop: 
     another_time = input("Would you like to roll another time?") 
     if another_time.lower() in ['n', 'no']: 
      sys.exit() 
    # This allows the code above to output on additional loops. 
    first_loop = False 
    return first_loop 

while True: 
    # How do I not reset this back to True each time the program loops? 
    first_loop = True 

    get_user_input(first_loop) 

    number_of_dice = int(input("Enter the number of dice you would like to roll: ")) 
    number_of_sides = int(input("Enter the number of sides for the dice: ")) 

    # create the dice_total list so each time we create a roll in the loop, 
    # it can be added to a list and the total calculated 
    dice_total = [] 

    for die in range(number_of_dice): 
     random_roll = random.randrange(1, number_of_sides) 
     print("You rolled: ", random_roll) 
     dice_total.append(random_roll) 

    dice_total = sum(dice_total) 
    print("The total of the dice rolled is: ", dice_total) 

答えて

2

あなたはかなり近いです。

# move this outside the loop 
first_loop = True 

while True: 
    if not first_loop: 
     get_user_input() 
    first_loop = False 

そしてget_user_input機能自体にfirst_loopを使用する必要はありません:True/Falseを返す方がよいし、代わりに機能してsys.exitを使用するに応じて行動するだろう

def get_user_input(): 
    another_time = input("Would you like to roll another time?") 
    if another_time.lower() in ['n', 'no']: 
     sys.exit() 

は(与えますあなたはもっとコントロールできます):

def get_user_input(): 
    another_time = input("Would you like to roll another time?") 
    return not another_time.lower() in ['n', 'no'] 

そして、あなたが行うことができます:

while True: 
    if not first_loop: 
     if not get_user_input(): 
      # break out of the loop 
      break 
+0

これはこれをグローバル変数にしませんか、グローバル変数を正しく理解できませんか? – Phorden

+1

このコードはグローバル変数を使用しません。 'first_loop'変数をループ外に移動して一度初期化します。一般的に言えば、 'global'キーワードはコードのモジュール化が少なくメンテナンス性が低いコード臭です。 –

+0

@ReutSharabaniグローバル変数は、関数/メソッドに対してローカルではない、つまりファイルスコープに存在する変数です。初期化変数をループから抜き出しても、それをグローバルにすることはできません。周囲の機能に対してはまだローカルです。 – antred

1

あなたはlistに変数を置くことができます。これにより、get_user_input()関数の値を変更し、グローバル変数にすることを避けることができます。

import sys 
import random 

def get_user_input(first_loop): 
    if not first_loop[0]: # access value in the list 
     another_time = input("Would you like to roll another time?") 
     if another_time.lower() in ['n', 'no']: 
      sys.exit() 
    # This allows the code above to output on additional loops. 
    first_loop[0] = False 
    return first_loop[0] # access value in the list 

while True: 
    # How do I not reset this back to True each time the program loops? 
    first_loop = [True] # change to a list 

    get_user_input(first_loop) 

    number_of_dice = int(input("Enter the number of dice you would like to roll: ")) 
    number_of_sides = int(input("Enter the number of sides for the dice: ")) 

    # create the dice_total list so each time we create a roll in the loop, 
    # it can be added to a list and the total calculated 
    dice_total = [] 

    for die in range(number_of_dice): 
     random_roll = random.randrange(1, number_of_sides) 
     print("You rolled: ", random_roll) 
     dice_total.append(random_roll) 

    dice_total = sum(dice_total) 
    print("The total of the dice rolled is: ", dice_total) 
+0

私はグローバル変数なしでこれを行うことを望んでいましたが、感謝します。 – Phorden

+0

グローバル変数なしで実行できます。更新された回答をご覧ください。 – martineau

+0

受け入れるには十分ではありませんか? – martineau