ピラミッド関数のアキュムレータ変数 "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()
私はこれらの 'try..except'ブロックの目的を見ません。 – TigerhawkT3
不正な入力があった場合、プログラムはクラッシュしません。そうすれば、ユーザーは何でも入力することができ、正解でない場合は間違ってマークするだけです。 –