Reputation: 51
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()
Upvotes: 1
Views: 7296
Reputation: 812
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
.
Upvotes: 2
Reputation: 494
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')
Upvotes: 1
Reputation: 174
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()
Upvotes: 2