使用变量调用函数时引发的值错误

2024-09-30 20:34:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着写一个简单的脚本,从一个单词开始,然后不断打印与前面的单词押韵的单词(即egg、aaberg、mpeg)。它使用NLTK。但是,在运行代码时,我得到一个错误:

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)

我把它缩小到一个函数,主函数,返回一个押韵单词列表。你知道吗

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

当我做押韵(“蛋”,1)它返回一个押韵词列表。没问题吧?但如果我这么做了:

x = "egg"
rhyme(x, 1)

我得到上面所说的错误。换句话说,当我使用一个变量时,它会抛出一个错误,我真的不知道为什么。你知道吗

完整代码:

# -*- 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)

所有的错误都是a<;而不是a>;:

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

Tags: intxtforlenreturniflevelreal
2条回答

您正在生成一个空范围:

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

因此,只有在word中有零个或一个元素时,才调用randint()。第二个参数将是0-1,并且randint(0, -1)对于该函数无效。你知道吗

你可能想改用>= 1。与其使用randint(),不如使用random.choice()从列表中选取随机元素:

if word:
   word_real = random.choice(word)

如果word列表不为空,则if word为真。你知道吗

这与是否使用变量无关。问题似乎在这里:

 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)

相关问题 更多 >