0

the program I had looks like this:

def wordToNumber(word):
if word == "one":
return 1
elif word == "two":
return 2
elif word == "three":
return 3
elif word == "four":
return 4
elif word == "five":
return 5
elif word == "six":
return 6
elif word == "seven":
return 7
elif word == "eight":
return 8
elif word == "nine":
return 9
elif word == "ten":
return 10

but since my professor is hella strict he's like...yes that works...but I'd like it in a while loop.

the .....monstrosity....that I've come up with so far that I know is completely wrong but bear with me im learning...and he's not that great of a teacher....

def wordToNumber(word):
wordList = ["zero","one","two","three",...,"nine"]
while word in wordList:
wordList == range(10)
print i in range(10)

:D...aha....yeah I know. fail....but all I need is someone to show me the proper way and then maybe i can learn not to be such a fail....lol.

please help me?

6
Contributors
10
Replies
11
Views
6 Years
Discussion Span
Last Post by pyTony
0

In your case it's actually much easier to use:

def wordtonum(word):
    wordList = ["zero","one","two","three","four","five","six","seven","eight","nine"]
    return int(wordList.index(word))

Edited by Kruptein: n/a

0

camigirl4k3,
you got to learn to use the necessary indentations with your statement blocks.

The indentations are maybe changed because camigirl4k3 doesn't use code tags

0

If indentations were used, you can see them by temporarily going to reply. In this case no code indentations at all.

0

So what is the program supposed to do? Are you supposed to input a number and get the word form of the number? Or input the word and get the integer it represents?
And incase you didn't know the code tags they are [COD'E] [/COD'E] with out the ' in them.

this is code......

Edited by Tech B: n/a

0

So what is the program supposed to do? Are you supposed to input a number and get the word form of the number? Or input the word and get the integer it represents?
And incase you didn't know the code tags they are [COD'E] [/COD'E] with out the ' in them.

this is code......

I think he means that you have to pass a word and convert it in an integer.

just like my code does ...

0

But like your code, if I don't search for a word that is in the list, it gives ne an index error. That's not converting anything, just spitting out the same word. I was thinking like the integer 10 to "ten"

0

But like your code, if I don't search for a word that is in the list, it gives ne an index error. That's not converting anything, just spitting out the same word. I was thinking like the integer 10 to "ten"

Okay if U give a number in words larger then nine it will throw an error,
but I just used the same example as the topic starter.

the thing you ask is the reverse, if U want that you have to do:

def numtoword(num):
    wordlist=["zero","one","two","three","four","five","six","seven","eight","nine"]
    if int(num)>len(wordlist):
        print "sorry that number is not in the list"
    else:
        return wordlist[int(num)]
0

<html>
<head></head>
<body>
<img src=<html>
<head></head>
<body>
<img src="math1.gif">
<script language="JavaScript">


document.write("<center><B>FIBONACCI</B></center>")
var f1=prompt("Enter NUmber From 1 to 19: ","0");

if(f1=='1')
(document.write("<center><b>1 based number</center></b>"));
if(f1=='2')
(document.write("<center><b>1+1<br>=<br>2</center></b>"));
if(f1=='3')
(document.write("<center><b>1+1+2<br>=<br>4</center></b>"));
if(f1=='4')
(document.write("<center><b>1+1+2+3<br>=<br>7</center></b>"));
if(f1=='5')
(document.write("<center><b>1+1+2+3+4<br>=<br>11</center></b>"));
if(f1=='6')
(document.write("<center><b>1+1+2+3+4+5<br>=<br>16</center></b>"));
if(f1=='7')
(document.write("<center><b>1+1+2+3+4+5+6<br>=<br>22</center></b>"));
if(f1=='8')
(document.write("<center><b>1+1+2+3+4+5+6+7<br>=<br>29</center></b>"));
if(f1=='9')
(document.write("<center><b>1+1+2+3+4+5+6+7+8<br>=<br>37</center></b>"));
if(f1=='10')
(document.write("<center><b>1+1+2+3+4+5+6+7+8+9<br>=<br>46</center></b>"));
if(f1== '11')
(document.write("<center><b>1+1+2+3+4+5+6+7+8+9+10<br>=<br>56</center></b>"));
if(f1== '12')
(document.write("<center><b>1+1+2+3+4+5+6+7+8+9+10+11<br>=<br>67</center></b>"));
if(f1== '13')
(document.write("<center><b>1+1+2+3+4+5+6+7+8+9+10+11+12<br>=<br>79</center></b>"));
if(f1== '14')
(document.write("<center><b>1+1+2+3+4+5+6+7+8+9+10+11+12+13<br>=<br>92</center></b>"));
if(f1== '15')
(document.write("<center><b>1+1+2+3+4+5+6+7+8+9+10+11+12+13+14<br>=<br>106</center></b>"));
if(f1== '16')
(document.write("<center><b>1+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15<br>=<br>121</center></b>"));
if(f1== '17')
(document.write("<center><b>1+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16<br>=<br>137</center></b>"));
if(f1== '18')
(document.write("<center><b>1+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17<br>=<br>154</center></b>"));
if(f1== '19')
(document.write("<center><b>1+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+<br>=<br>172</center></b>"));
</script>
</body>
</html>

Editor's note:
Wrong forum!

Edited by vegaseat: note

0

Some problem with your function included down. Strange, the printed message comes only from calling the function from command line, not in loop going over range.

>>> numtoword('11')
sorry that number is not in the list
>>> ================================ RESTART ================================
>>> 
----------
-1 nine
0 zero
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10

Traceback (most recent call last):
  File "D:/Tony/numtoword.py", line 27, in <module>
    print i,numtoword(i)
  File "D:/Tony/numtoword.py", line 12, in numtoword
    return wordlist[int(num)]
IndexError: list index out of range
>>>

After limiting range until 9 for that loop (range(-1,10))

>>> 
----------
-1 nine
0 zero
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
----------
-1 None
0 zero
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
11 eleven
----------
-1 None
0 nolla
1 yksi
2 kaksi
3 kolme
4 neljä
5 viisi
6 kuusi
7 seitsemän
8 kahdeksan
9 ykdeksän
----------
>>>

Here is my version numtoword2 after your old version

# -*- coding: latin1 -*-
english=["zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven"]
finnish=["nolla","yksi","kaksi","kolme","neljä","viisi","kuusi","seitsemän","kahdeksan","ykdeksän"]

languages=[english,finnish]

def numtoword(num):
    wordlist=["zero","one","two","three","four","five","six","seven","eight","nine"]
    if int(num)>len(wordlist):
        print "sorry that number is not in the list"
    else:
        return wordlist[int(num)]

def numtoword2(num):
    if num in range(len(wordlist)):
        return wordlist[int(num)]
##    else:
##        return '****'
##        return -1
##        raise IndexError,str(num)+" not in proper range (0.."+str(len(wordlist))+')'

print '-'*10

## we do not know how many words the function knows (local variable)

## range starting from -1 just to test misuse of function

for i in range(-1,10):     ##range(-1,11):
    print i,numtoword(i)

print '-'*10

for wordlist in languages:
    for i in range(-1,len(wordlist)):
        print i,numtoword2(i)
    print '-'*10
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.