Home > Blockchain >  Why can't I call a function as parameter of other functions with the same return values as para
Why can't I call a function as parameter of other functions with the same return values as para

Time:07-25

I am a working on a program that should take a level input 1, 2, or 3 and depending on that input will generate a random integer of one two or three digits (in integer_generator function). I then will take those random integers and put them into 10 math equations for the user to solve (in question_generator). My two problems lie when I try calling these functions together all at once so I generate a question with question_generator() that takes the parameter of integer_generator(), and integer_generator() must have the parameter of get_level() in order to randomly produce integers according to the input the user provided. My first problem is the error message thrown when I run my code that has a problem with the function calling of other functions as parameters.

Traceback (most recent call last):
  File "/workspaces/107595329/professor/professor.py", line 52, in <module>
    ten_questions()
  File "/workspaces/107595329/professor/professor.py", line 49, in ten_questions
    question_generator(integer_generator(get_level()))
TypeError: question_generator() missing 1 required positional argument: 'y'

My second problem is that in the event when I get the functions to run as parameters to each other as desired, the user will be prompted with a need to input a level each time after every question as opposed to only having to input the level once and having the computer generate ten problems as a result but ask them on a question by question basis. For example this is how it would work right now if the functions called each other without the error message:

Level: 1
2   9 = 11
Level: 1
3   6 = 9
Level: 1
4   5 = 9

as opposed to the desired:

Level: 1
2   9 = 11
3   6 = 9
4   5 = 9

Here is the code and my issue line is line 49:

import random

def main():
    asdf = "this is just so it doesn't throw an error"

def get_level():
    while True:
        try:
            level_input = int(input("Level: "))
            if level_input in [1,2,3]:
                break
        except:
            pass

        return level_input
def integer_generator(level):
    if level == 1:
        x = random.randint(1,9)
        y = random.randint(1,9)
    elif level == 2:
        x = random.randint(10, 99)
        y = random.randint(10, 99)
    else:
        x = random.randint(100, 999)
        y = random.randint(100, 999)
    return x, y



def question_generator(x, y):
    real_answer = x   y
    while True:
        try:
            answer_given =  input(str(x)   "   "   str(y)   " = ")
            if int(answer_given) == real_answer:
                print("Correct")
                break
            else:
                print("EEE")
                pass
        except:
            print("EEE")
            pass


def ten_questions():
    num_of_questions = 0
    while num_of_questions <= 10:
        question_generator(integer_generator(get_level()))
        num_of_questions  =1

ten_questions()

if __name__ == "__main__":
    #main()
    dd = "this is just so it doesn't throw an error"

CodePudding user response:

integer_generator return a tuple, If you want Python to unpack your tuple into two separate arguments, use the * operator:

    question_generator(*integer_generator(get_level()))

To ask the question n times without calling every time the get_level() method, you should store the return value of get_level() before entering the while loop, in this way:

def ten_questions():
    num_of_questions = 0
    my_level = get_level()
    while num_of_questions <= 10:
        question_generator(*integer_generator(my_level))
        num_of_questions  =1
  • Related