Python Forum
Function to convert - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Function to convert (/thread-5802.html)



Function to convert - slehee - Oct-22-2017

How to write a function that takes a single parameter ( for example numbers from 1-10) then returns a string like "one" to number 1 and "two" to number 2.

def numberconvert(x):
    if x== 1;
    return "one"
somehow it doesn't look to be alright.
Should a try to use a list=["one,"two",...]?


RE: Function to convert - metulburr - Oct-22-2017

i would use a dictionary to associate the number with the text. In this wa yyou wouldnt even really need a function as there wouldnt be much code. Just return dict[NUM_AS_INT] to get the text num


RE: Function to convert - slehee - Oct-22-2017

The task is to write a function which will associate the number with a text, so have to make one.


RE: Function to convert - sparkz_alot - Oct-22-2017

Watch your typing, the end of your "if" statement should be a colon, not a semi-colon. Also, please get away from using single letter variables and use descriptive names instead. Metulburr is not saying you can't use a dictionary in a function, just that you could. That is, if you are allowed to use dictionary's. Did the instruction say how large a number your function should handle? Would it be 10? If so, a list or dictionary could be used. How would do it with a list?


RE: Function to convert - slehee - Oct-22-2017

Yes, I did mistype the semicolon, The instruction is to write a function which asks for a single parameter, in my case a number and it will return a string( for number 1="one") it should handle numbers from 1-10.
After that, i have to use this function in another function what I have made already

def count(numOne, numTwo):
for i in range (numOne,numTwo+1):
print i


print count(2,8)

and the results should be instead of 2,3,4,5,6,7,8, one two three...


RE: Function to convert - sparkz_alot - Oct-22-2017

Please repost your code using the the code tags so we can see your indentation. See the link in your first post. Also you say you function can only take a single argument, you are providing 2. Where is the list?


RE: Function to convert - slehee - Oct-22-2017

def numberToText(x):
if x==1:
return "one"
elif x==2:
return "two"
elif x==3:
return "three"

def count(numOne,numTwo):
numberToText()
for i in range (numOne,numTwo):
print i


print count(1,3)


RE: Function to convert - sparkz_alot - Oct-22-2017

You won't be asked again to use the code tags.

You now have two functions, the primary one still using two arguments.