Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Small function
#1
Can anyone help me with this exercise, please? I would appreciate it!
WRITE A FUNCTION:
● That takes two arguments as its input
○ A list
○ A number
● And returns
○ True if the length of the list is greater than the number that was passed in as the second
argument

def func_11(a_list,a_number):
    if len(a_list) > a_number:
        a = True
    return a
THAT does not work!!
Thanks a lot in advance!!
Reply
#2
It's best to explain in what way does it not work. Is there some input for which there is an except? If so, provide the input and the full stack trace. If not, is there an input for which there is an undesirable output? It's best to provide the input, desired output, and erroneous output, labeled clearly.

In this case, I can tell that when your if-condition is False, you'll get a NameError. You can either create an else-branch to ensure that [icode]a[/code] is always defined, or you can just return the expression that you're using and skip the whole if-else and variable name. In this case, I suggest you add an else-branch unless you're comfortable with understanding everything I've said here.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020