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()
Izy
  • 51
  • 1
  • 9
  • replace last line with **main()** – Ghanteyyy May 06 '22 at 04:29
  • I cannot reproduce the problem. That said, there are several other problems with this code. Please read [ask] and https://stackoverflow.com/help/minimal-reproducible-example and https://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? – Karl Knechtel May 06 '22 at 04:30

3 Answers3

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.

Mohamed Yasser
  • 641
  • 7
  • 17
  • Awesome, thanks now I just have to figure out how to fix the name error. – Izy May 06 '22 at 04:37
  • 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). – Mohamed Yasser May 06 '22 at 04:38
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()
Raj Patel
  • 154
  • 6
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')
Ghanteyyy
  • 484
  • 1
  • 5
  • 12