So I can't really figure out why the variables "first" and "second" aren't defined even when returning them it doesn't work maybe I'm doing it wrong I honestly have no clue. The assignment is to make a times table tester using only functions to give us an understanding of functions. My teacher gave us what the functions are supposed to do so I will put these below.
def results_summary(right, answered): """ right is int for number of correct questions answered is int rep. total number of questions display results of practice with ratio and percentage correct """
def generate_question(): """ generate and display a random multiplication questions with factors from 1 to 12 return the answer as an integer value """
def get_answer(): """ display = to prompt user to enter answer returns input if valid integer entry is made displays message and new prompt for invalid entry """
def goAgain(): """ Asks the user y/n if they want another question returns True for 'y' and False for 'n' displays message and new prompt for invalid entry """
Any help would be greatly appreciated!
import random
#defines important functions
def results_summary(num_correct, num_question):
percentage = num_correct / num_question * 100
correctCount = str(num_correct)
questionCount = str(num_question)
percentage = str(round(percentage))
print('You got ' correctCount '/' questionCount ' (' percentage '%).')
def generate_question():
first = random.randint(1,12)
second = random.randint(1,12)
correct_answer = second * first
return correct_answer
def get_answer():
try:
user_answer = input('What is ' first ' x ' second '?')
except ValueError:
print('Please Enter Integers Only.')
def goAgain():
input('Do you want another question? (y / n)')
try:
'y' == True
'n' == False
except ValueError:
print('Please Enter a valid response. (y / n)')
#MAIN PROGRAM
num_correct = 0
num_question = 0
#creates while loop to continuously ask questions
while True:
correct_answer = generate_question()
user_answer = get_answer()
#prints if the user answers correctly
if user_answer == correct_answer:
print('Correct')
num_question = 1
num_correct = 1
again = goAgain()
if goAgain == False:
break
results_summary(num_correct, num_question)
CodePudding user response:
The error shows variables first and second to be undefined because the variables you are using in:
user_answer = input('What is ' first ' x ' second '?')
command inside the get_answer() function, are undefined.
In order to use these variables, just change the function generate_question() to:
def generate_question():
global first, second
first = random.randint(1,12)
second = random.randint(1,12)
correct_answer = second * first
return correct_answer
and change the input command inside get_answer() function to:
user_answer = input('What is ' str(first) ' x ' str(second) '?')
CodePudding user response:
I'm so sorry budd :(
I got no time to explain ... [I'm in a hurry] but I don't want you to get disrespect from your teacher !, I been there :/
So here is the modified working code , hope it helps :)
import random
#defines important functions
def results_summary(num_correct, num_question):
percentage = (num_correct / num_question) * 100
print(f"You got {round(percentage)} %")
def generate_question():
global first
global second
first = random.randint(1,12)
second = random.randint(1,12)
correct_answer = second * first
return correct_answer
def get_answer():
try:
user_answer = int(input('What is ' str(first) ' x ' str(second) '?'))
return user_answer
except ValueError:
print('Please Enter Integers Only.')
def goAgain():
confo = input('Do you want another question? (y / n)')
if confo.lower() == 'y':
return True
elif confo.lower() == "n":
return False
else:
return
#MAIN PROGRAM
num_correct = 0
num_question = 0
#creates while loop to continuously ask questions
while True:
correct_answer = generate_question()
user_answer = get_answer()
#prints if the user answers correctly
if user_answer == correct_answer:
print('Correct')
num_question = 1
num_correct = 1
else:
num_question = 1
print("Wrong answer")
again = goAgain()
if again:
pass
else:
print("Have a great day!, bye")
break
if goAgain == False:
break
results_summary(num_correct, num_question)
