-3

I am attempting a practice question on Coursera where the code seems to be throwing me off even though the logic seems fine to me. I am a beginner trying to get my hand on the language but I am running into logical errors.

def is_positive(number):
  if number > 0:
    return "True"
  else:
    return "None"

number=-1
print(is_positive(number))
Kevin
  • 300
  • 2
  • 13
  • What exactly is the question here? Your code looks fine. – Kevin Jan 23 '23 at 02:37
  • 1
    "I am running into logical errors" does not help explain the problem. Show us what the code **actually did**, and tell us what you **were expecting** instead. – John Gordon Jan 23 '23 at 02:41
  • Hi Guys! So I have to fill in the blanks in the below code to show True for a positive value and None for any other value. def is_positive(number): if ---: return --- On running the code I wrote I am getting the following error: Not quite, is_positive(-5) returned None instead of None. Remember that positive numbers are those bigger than zero (x > 0). – Suckerpunch093 Jan 23 '23 at 02:54
  • @Suckerpunch093 What do you mean by "None instead of None"? Do you want to return the values "True" and "None" instead of the strings? – Kevin Jan 23 '23 at 02:58
  • Welcome to Stack Overflow. When following along with courses like Coursera that expect you to solve problems and do automatic testing of the code, it's important to understand the input and output specification **exactly**. Here, the problem is that they want you to return the special Python value `None`, **not** a string with that text. Please see the linked duplicate to understand what `None` is. I am guessing the same applies for `True` rather than `"True"`. More importantly, make sure you understand the concept of **type**. It is impossible to write sensible code otherwise. – Karl Knechtel Jan 23 '23 at 03:24
  • To help with future questions, please read [ask]. We don't care about your level of experience, your frustration with the process, or anything else about you. We **do** care about clear descriptions of problems (i.e. "when I try this code ... and give it to an automated tester, it tells me ...") and clear, **direct** questions (i.e. "how do I make the code ?"). – Karl Knechtel Jan 23 '23 at 03:26
  • From the comment on the accepted answer, it seems that you may have already understood the necessary material. Please try to find problems like this before posting (here is a useful resource: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At the very least, try to explain them clearly; while writing it out, you may realize what is wrong already. – Karl Knechtel Jan 23 '23 at 03:28

2 Answers2

-1

From your comment, I assume that you want to return the values True and None, rather than the strings "True", and "None". To do this, simply remove the quotation marks surrounding the string.

def is_positive(number):
  if number > 0:
    return True
  else:
    return None

number=-1
print(is_positive(number))
Kevin
  • 300
  • 2
  • 13
-2
n = int(input())
print( True if n > 0  else False)

I think this is what you're looking for

Charles
  • 11
  • 2