2016-07-02 3 views
0

ピラミッド関数のアキュムレータ変数 "points"のデータを統計関数に渡そうとしています。私がやったやり方は、0の元の "points"の値を渡すだけです。また、統計を受け取ってピラミッド関数とは別に実行したいのです。今のところ、ピラミッド関数内からの印刷で統計情報が処理されます。ある関数から別の関数に変数からのデータを送信して、後で他の関数を呼び出す必要がある場合はどうすればよいですか?統計情報が呼び出されると、再生されるいくつかのゲームにまたがってプレイヤーに関するいくつかの情報が表示されます(合計点や間違った質問の数など)。後で実装されます。それを動作させるためにある関数から別の関数にデータを渡す際に助けが必要

import random 
from random import choice 
from random import randint 

def pyramid(): 
    for k in range (1,3): 
     print('\nPractice Problem', k, 'of 2') 
     min_pyramid_size = 3 
     max_pyramid_size = 5 
     total_chars = 0 
     num_rows = random.randint(min_pyramid_size, max_pyramid_size) 
     for i in range(num_rows): 
      x = ''.join(str(random.choice('*%')) for j in range(2*i+1)) 
      print(' ' * (num_rows - i) + x) 
      total_chars = total_chars + x.count('%') 
     try: 
      user_answer = int(input('Enter the number of % characters' + \ 
            'in the pyramid: ')) 
     except: 
       user_answer = print() 
     if user_answer == total_chars: 
      print('You are correct!') 
     else: 
      print("Sorry that's not the correct answer") 
    for k in range (1,11): 
     print('\nProblem', k, 'of 10') 
     points = 0 
     min_pyramid_size = 3 
     max_pyramid_size = 5 
     total_chars = 0 
     num_rows = random.randint(min_pyramid_size, max_pyramid_size) 
     for i in range(num_rows): 
      x = ''.join(str(random.choice('*%')) for j in range(2*i+1)) 
      print(' ' * (num_rows - i) + x) 
      total_chars = total_chars + x.count('%') 
     try: 
      user_answer = int(input('Enter the number of % characters' + \ 
            'in the pyramid: ')) 
     except: 
       user_answer = print() 
     if user_answer == total_chars: 
      print('You are correct!') 
      points +=1 
     else: 
      print("Sorry that's not the correct answer") 
    statistics(points) 




def statistics(points): 
    incorrect = 10 - (points) 
    print (points) 
    print (incorrect) 

pyramid() 
+0

私はこれらの 'try..except'ブロックの目的を見ません。 – TigerhawkT3

+0

不正な入力があった場合、プログラムはクラッシュしません。そうすれば、ユーザーは何でも入力することができ、正解でない場合は間違ってマークするだけです。 –

答えて

0

迅速なハック(ただし、それを行うための提案方法は)だから、

global points = 0 
# and so on 
1

なる関数内でグローバル変数pointspyramid()

を宣言されるだろうしたがって、以下は、コードが重複したコードを削除するために別々の関数に再加工されただけです。

以下のコードでは、コード行をコメントアウトして数値を指定していますが、これらはオプションです(ただしこれに限定されません)。あなたが選択した方法に応じて、数字に対応する行をコメントアウトして他のものを削除したい場合は

オプション1はyieldを実装します。これにより、関数ask_questionsを反復してポイントの現在の値を返すことができます。ここでは、関数に戻って質問を続ける前に、関数の外の値を扱うことができます。

オプション2の場合、これはただの点の最終値を返しますし、あなたがそれを格納し、私は完全ので、ここで質問を理解していない可能性があり

from random import choice 
from random import randint 

def create_pyramid(): 
    min_pyramid_size = 3 
    max_pyramid_size = 5 
    num_rows = randint(min_pyramid_size, max_pyramid_size) 
    pyramid_str = '' 

    for i in range(num_rows): 
     line = ''.join(str(choice('*%')) for j in range(2*i+1)) 
     pyramid_str += ' ' * (num_rows - i) + line + '\n' 

    return pyramid_str 

def get_input(): 
    """Will keep prompting user until an int is entered""" 
    while True: 
     user_answer = input('Enter the number of % characters in the pyramid: ') 
     if user_answer.isdigit(): 
      return int(user_answer) 
     else: 
      print("Sorry, that is invalid") 

def ask_questions(text, num): 
    points = 0  
    for k in range (1, num + 1): 
     print('\n{0} {1} of {2}'.format(text, k, num)) 

     pyramid = create_pyramid() 
     print(pyramid) 

     if get_input() == pyramid.count("%"): 
      print('You are correct!') 
      points += 1 
##   yield points # 1 
     else: 
      print("Sorry that's not the correct answer") 
##   yield points # 1 

## return points # 2 

def statistics(points, total): 
    print("Score: {0} of {1}".format(points, total)) 

def main(): 
    # 1 
## for _ in ask_questions('Practice Problem', 2): 
##  pass # since we just want to ignore the points they get 
##  
## i = 1 
## for points in ask_questions('Problem', 10): 
##  statistics(points, i) 
##  i += 1 

    # 2 
## ask_questions('Practice Problem', 2) 
## points = ask_questions('Problem', 10) 
## statistics(points) # your function code for this call 

if __name__ == '__main__': 
    main() 

別の関数にそれを渡すことができるようになりますと、別の例であります

def main(): 

    print("Welcome to ... \n") 

    ask_questions('Practice Problem', 2) 
    totals = [] 
    while True: 
     totals.append(ask_questions('Problem', 10)) 
     user_input = input("Would you like to play again [Y] or [N]?") 
     if user_input.lower() != 'y': 
      break 

    statistics(points) 

トップレベルの関数では、ユーザーがプログラムを実行中に得たすべての最終得点を含むリストがあります。 intではなくリストを使用するために統計を変更する必要があります。しかし、あなたがプレイしたすべてのゲームの結果を保持しながら、この方法で複数のゲームを持つことができます。

私が得ようとしているのは、生成、処理、表示するさまざまなことを処理するための複数の機能があります。この方法で、すべてのデータを追跡できる1つのトップレベル関数の下ですべてをグループ化できます。

+0

@the_martian、この回答があなたの問題の解決に役立つと感じたら、緑色のチェックマークをクリックして「受け入れ済み」とマークしてください。これはコミュニティが未回答の質問に焦点を当てるのに役立ちます。 – Lahiru

関連する問題