피라미드 함수의 accumulator 변수 "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
잘못된 입력을 입력하면 프로그램이 충돌하지 않습니다. 그렇게하면 사용자는 아무 것도 입력 할 수 없으며 정답이 아닌 경우 잘못 표시합니다. –