1

I just started studying UDF's and I can't get my code to work and I am unsure what I did wrong, anyone know how to fix this?

I am getting this error line 55, in main() TypeError: main() missing 3 required positional arguments: 'word1', 'word2', and 'word3'

my code looks like this

import random

def get_determiner(amount):
    if amount == 1:
        determiner = ['a', 'one', 'the']
    else:
        determiner = ['those', 'some', 'many', 'the']
    word1 = random.choice(determiner)
    return word1

def get_noun(amount):
    if amount == 1:
        noun = ["bird", "boy", "car", "cat", "child",
        "dog", "girl", "man", "rabbit", "woman"]
    else:
        noun = ["birds", "boys", "cars", "cats", "children",
        "dogs", "girls", "men", "rabbits", "women"]

    word2 = random.choice(noun)
    return word2

def get_verb(amount, tense):
    if tense == 'past':
        verb = ["drank", "ate", "grew", "laughed", "thought",
        "ran", "slept", "talked", "walked", "wrote"]
    elif tense == 'past' and amount == 1:
        verb = ["drinks", "eats", "grows", "laughs", "thinks",
        "runs", "sleeps", "talks", "walks", "writes"]
    elif tense == 'past' and amount != 1:
        verb =["drink", "eat", "grow", "laugh", "think",
        "run", "sleep", "talk", "walk", "write"]
    elif tense == 'future':
        verb =["will drink", "will eat", "will grow", "will laugh",
        "will think", "will run", "will sleep", "will talk",
        "will walk", "will write"]
    
    word3 = random.choice(verb)
    return word3

def main():

    get_determiner(word1)
    get_noun(word2)
    get_verb(word3)

    amount = input('how many things are there? ')
    tense = input('Past, present or future? ')
    first = word1.capitalize()

    print(f'{first} {word2} {word3}')

main()
2
  • replace last line with main() Commented May 6, 2022 at 4:29
  • I cannot reproduce the problem. That said, there are several other problems with this code. Please read How to Ask and stackoverflow.com/help/minimal-reproducible-example and meta.stackoverflow.com/questions/359146. It's not clear to me, however, that you understand how calling a function works. If you are confused about it, please try following a tutorial. If you do understand, then think about it more clearly: where should e.g. the amount parameter for get_determiner come from? What should happen first - asking the user for this information, or calling the function? Commented May 6, 2022 at 4:30

3 Answers 3

2
import random

def get_determiner(amount):
    if amount == 1:
        determiner = ['a', 'one', 'the']
    else:
        determiner = ['those', 'some', 'many', 'the']
    word1 = random.choice(determiner)
    return word1

def get_noun(amount):
    if amount == 1:
        noun = ["bird", "boy", "car", "cat", "child",
        "dog", "girl", "man", "rabbit", "woman"]
    else:
        noun = ["birds", "boys", "cars", "cats", "children",
        "dogs", "girls", "men", "rabbits", "women"]

    word2 = random.choice(noun)
    return word2

def get_verb(amount, tense):
    if tense == 'past':
        verb = ["drank", "ate", "grew", "laughed", "thought",
        "ran", "slept", "talked", "walked", "wrote"]
    elif tense == 'past' and amount == 1:
        verb = ["drinks", "eats", "grows", "laughs", "thinks",
        "runs", "sleeps", "talks", "walks", "writes"]
    elif tense == 'past' and amount != 1:
        verb =["drink", "eat", "grow", "laugh", "think",
        "run", "sleep", "talk", "walk", "write"]
    elif tense == 'future':
        verb =["will drink", "will eat", "will grow", "will laugh",
        "will think", "will run", "will sleep", "will talk",
        "will walk", "will write"]
    
    word3 = random.choice(verb)
    return word3

def main():



    amount = input('how many things are there? ')
    tense = input('Past, present or future? ')
    word1 = get_determiner(amount)
    word2 = get_noun(amount)
    word3 = get_verb(amount, tense)
    first = word1.capitalize()

    print(f'{first} {word2} {word3}')

    
main()
Sign up to request clarification or add additional context in comments.

Comments

2

First of all, you are using 3 variables word1 word2 word3 inside main() , but main doesn't know who these variables are, so either define the three of them as global variables (highly discouraged in programming), pass them as arguments to main() ,or define them inside main().

Second, print() is a function that doesn't return anything, so passing print() to main() is like passing None type to a function. It doesn't make much sense.

Third, your get_verb() takes two arguments, but you only pass one. Pass the correct arguments or use a default value for the other argument.

Fourth, when calling your three functions inside main() you are not storing the return in any variable, it's almost like you didn't call them.

Fix these and try again, because the error you should be getting is a NameError: name 'first' is not defined and not a TypeError.

2 Comments

Awesome, thanks now I just have to figure out how to fix the name error.
Just define the variables inside the function (like in Raj Patel's answer), pass them as arguments, or make them global (don't do that one).
1

You are passing parameters in main function when it does not have any. Learn more about arguments and parameters

So you can change your main function

def main(word1, word2, word3):
….

And call the main function

main('word1', 'word2', 'word3')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.