Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am using Python 3.7 and I have some questions about my code.
what does "returning the string only" mean? If a function is just returning the string, how can I print that - returned string at the end of my code?
For example, if I write the following code:

def word(num):
  d1= {0:'Zero',1:'One',2:'Two',3:'Three',4:'Four',5:'Five',6:'Six',7:'Seven',8:'Eight',9:'Nine',10:'Ten',11:'Eleven',12:'Twelve',13:'Thirteen',14:'Fourteen',15:'Fifteen',16:'Sixteen',17:'Seventeen',18:'Eighteen',19:'Ninteen',20:'Twenty',30:'Thirty',40:'Fourty',50:'Fifty',60:'Sixty',70:'Seventy',80:'Eighty',90:'Ninty'}

  if num >= 100 or num <= -100:
    return "This number is out of range"
  elif num < 0:
    return "This number is less than 0 and it's spelling is Minus " + word(-num)
  elif num < 20:
    return d1[num]
  else:
    if num % 10 == 0:
      return d1[num]
    else:
      return d1[num // 10 * 10] + ' ' + d1[num % 10]



print (word(- 100))
print (word(- 42))
print (word(13))



Here the function is printing as well. But if I want to return the string from this function and want to print the returned string outside of this function, how can I do that?
The way I tried, is it right?

What I have tried:

def main():
print (word(-100))
print(word(-42))
print(word(13))
Posted
Updated 9-Feb-19 3:18am
Comments
Richard MacCutchan 9-Feb-19 5:47am    
Did you try it? It works fine for me.
CPallini 9-Feb-19 9:20am    
Of course it works fine. It is my own code... :-D
https://www.codeproject.com/Answers/1276464/Writing-function-giving-input-and-printing-them-wi
Richard MacCutchan 9-Feb-19 12:39pm    
lol

1 solution

Quote:
print (word(- 100))
print (word(- 42))
print (word(13))

The above code is not part of the word function, it is in the global scope (it is NOT indented).


If you define
Python
def main():
  print (word(-100))
  print(word(-42))
  print(word(13))
then you have to call it in the global scope, in order to see any output:
Python
main()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900