26107

why does my function is returning data type None??: Python datatype None

Question:

This is my python code for printing an absolute number. My function is returning type None. I am not getting what I have done wrong. Please help me.

def n(num): if num<0: return (num*-1) no = input("Enter a number: ") print "Absolute Value is: " print n(no)

Answer1:

def n(num): if num<0: return (num*-1) else: return num no = input("Enter a number: ") print "Absolute Value is: " print n(no)

writing an else statement will return num >= 0

Thank You :)

Answer2:

The return is on the condition. Try :

def n(num): if num<0: num *= -1 return num no = int(input("Enter a number: ")) print "Absolute Value is: " print n(no)

Answer3:

You really don't need your own function for absolute value...

no = input("Enter a number: ") print "Absolute Value is: ", abs(no)

Recommend

  • Exporting SAS DataSet on to UNIX as a text file…with delimiter '~|~'
  • Fraction length
  • Retrieving values from a PHP Multi-dimensional Array
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • import scipy.sparse failed
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • Grails calculated field in SQL
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Textfile Structure (tables)
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Meteor: Do Something On Email Verification Confirmation
  • How to add date and time under each post in guestbook in google app engine
  • MySQL WHERE-condition in procedure ignored
  • Where to put my custom functions in Wordpress?
  • JSON with duplicate key names losing information when parsed
  • Do I've to free mysql result after storing it?
  • Return words with double consecutive letters
  • RestKit - RKRequestDelegate does not exist
  • WPF Applying a trigger on binding failure
  • Benchmarking RAM performance - UWP and C#
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Acquiring multiple attributes from .xml file in c#
  • C# - Getting references of reference
  • How do I configure my settings file to work with unit tests?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • How can I remove ASP.NET Designer.cs files?
  • costura.fody for a dll that references another dll
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?