使用变量调用函数时的ValueError

时间:2015-06-01 15:19:04

标签: python variables nltk

我正在尝试编写一个以单词开头的简单脚本,然后继续打印与之前的单词押韵的单词(即egg,aaberg,mpeg)。它使用NLTK。但是在运行代码时出现错误:

dsd

我已将其缩小为一个函数,即主函数,它返回一个押韵的单词列表。

Traceback (most recent call last):
   File "C:\Users\myname\Google Drive\Python codes\Rhyming words.py", line 58, in <module>
     word_real = word[randint(0, len(word)-1)]
   File "C:\Python27\lib\random.py", line 242, in randint
     return self.randrange(a, b+1)
   File "C:\Python27\lib\random.py", line 218, in randrange
    raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)

ValueError: empty range for randrange() (0,0,0)

当我做押韵时(&#34; egg&#34;,1),它会返回一个押韵词汇列表。没问题吧?但如果我这样做:

def rhyme(inp, level):
     entries = nltk.corpus.cmudict.entries()
     syllables = [(word, syl) for word, syl in entries if word == inp]
     rhymes = []
     for (word, syllable) in syllables:
             rhymes += [word for word, pron in entries if pron[-level:] == syllable[-level:]]
     return rhymes

我收到上述错误。换句话说,当我使用变量时它会抛出一个错误而我真的不知道为什么。

完整代码:

x = "egg"
rhyme(x, 1)

所有错误的是&lt;而不是&gt;在:

# -*- coding: cp1252 -*-
import nltk, time, os
from random import randint

###Words###

import urllib2

word_site = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"

response = urllib2.urlopen(word_site)
txt = response.read()
WORDS = txt.splitlines()

###end WORDS###

def rhyme(inp, level):
     entries = nltk.corpus.cmudict.entries()
     syllables = [(word, syl) for word, syl in entries if word == inp]
     rhymes = []
     for (word, syllable) in syllables:
             rhymes += [word for word, pron in entries if pron[-level:] == syllable[-level:]]
     return rhymes

def text_file(mode):
     if os.path.isfile("words.txt"):
          words = open("words.txt", mode)
     else:
          words = open("words.txt", "w")
     return words

def start_word():
     words = text_file("r")
     if open("words.txt", "r").readlines() == 0:
          return WORDS[randint(0, len(WORDS)-1)]
     else:
          word = words.readlines()[len(words.readlines())-1]
          return word[0:len(word)-2]
     words.close()

def last_word(last_word):
     words = text_file("a")
     words.write(last_word+"\n")
     words.close()



word_start = start_word()

#debug
print word_start, type(word_start)

while True:
     word = rhyme(word_start, 1)
     #debug
     print word

     if (len(word)-1) < 1:
          word_real = word[randint(0, len(word)-1)]

          print word_real
          last_word(word_real)
          word_start = word_real

          time.sleep(0.3)

2 个答案:

答案 0 :(得分:1)

这与使用变量没有任何关系。问题似乎在这里:

 if (len(word)-1) < 1:
      word_real = word[randint(0, len(word)-1)]

只有在 len(word)-1) < 1时执行此部分代码,即您执行randint(0, 0)

您可能只是错误地使用了<而不是>

 if (len(word)-1) > 1:
      word_real = word[randint(0, len(word)-1)]

或更短:

 if word:
      word_real = random.choice(word)

答案 1 :(得分:1)

您在这里生成一个空范围:

if len(word)-1) < 1:
   word_real = word[randint(0, len(word)-1)]

所以,只有当word 中有零或一个元素时,才会调用randint()。第二个参数将是0-1randint(0, -1)对该函数无效。

您可能打算使用>= 1代替。而不是使用randint(),而是使用random.choice()从列表中选择一个随机元素:

if word:
   word_real = random.choice(word)
如果if word列表不为空,则

word为真。