Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从包含5个字母或更少的单词列表中打印单词Python 2.7?_Python_List_Python 2.7 - Fatal编程技术网

如何从包含5个字母或更少的单词列表中打印单词Python 2.7?

如何从包含5个字母或更少的单词列表中打印单词Python 2.7?,python,list,python-2.7,Python,List,Python 2.7,我对python有点陌生,你可能可以从我下面糟糕的代码中看到这一点。如果你玩这个游戏,你可能会发现它真的很难,所以为了让它更容易,我想让它只给用户列表中5个字母或更少的单词。我目前的代码是: word_site = "http://www.instructables.com/files/orig/FLU/YE8L/H82UHPR8/FLUYE8LH82UHPR8.txt" response = urllib2.urlopen(word_site) txt = response.read() WO

我对python有点陌生,你可能可以从我下面糟糕的代码中看到这一点。如果你玩这个游戏,你可能会发现它真的很难,所以为了让它更容易,我想让它只给用户列表中5个字母或更少的单词。我目前的代码是:

word_site = "http://www.instructables.com/files/orig/FLU/YE8L/H82UHPR8/FLUYE8LH82UHPR8.txt"
response = urllib2.urlopen(word_site)
txt = response.read()
WORDS = txt.splitlines()
randomword=(random.choice(WORDS))
目前,上面的代码从单词列表中获取任何随机单词,我希望它只获取包含5个或更少字母的单词

以下是我的所有代码,如果对您有帮助的话:

import random
import urllib2
loop=True
while loop==True:
    name=raw_input("Hello there! What might your name be? ")
    yesorno=raw_input("Hello " + name + ", would you like to play a friendly game of Guess the Word? Answer either 'yes' or 'no': ")
    if yesorno == "yes":
        loop=False
        print "Ok then, here we go!"
        print "\n"
        print "You have 3 guesses"
        word_site = "http://www.instructables.com/files/orig/FLU/YE8L/H82UHPR8/FLUYE8LH82UHPR8.txt"
        response = urllib2.urlopen(word_site)
        txt = response.read()
        WORDS = txt.splitlines()
        randomword=(random.choice(WORDS))
        lengthrandomword=len(randomword)
        print "I'm thinking of a word with " + str(lengthrandomword) + " letters"
        print "\n"
        if lengthrandomword==2:
            print "_ _"
            print "\n"
        elif  lengthrandomword==3:
            print "_ _ _"
            print "\n"
        elif  lengthrandomword==4:
            print "_ _ _ _"
            print "\n"
        elif  lengthrandomword==5:
            print "_ _ _ _ _"
            print "\n"
        elif  lengthrandomword==6:
            print "_ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==7:
            print "_ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==8:
            print "_ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==9:
            print "_ _ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==10:
            print "_ _ _ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==11:
            print "_ _ _ _ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==12:
            print "_ _ _ _ _ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==13:
            print "_ _ _ _ _ _ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==14:
            print "_ _ _ _ _ _ _ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==15:
            print "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _"
            print "\n"
        else:
            print "I thought of a word and it's reeeaaaaaly long, you would never guess it, so I'm just gonna tell you that my word was " + randomword
        print "\n"
        userguess=raw_input("Try and guess the word: ")
        input = randomword
        output = ""
        for i in input.split():
            output += i[0]
        firstletter=output
        if randomword.endswith("a"):
            lastletter="a"
        elif randomword.endswith("b"):
            lastletter="b"
        elif randomword.endswith("c"):
            lastletter="c"
        elif randomword.endswith("d"):
            lastletter="d"
        elif randomword.endswith("e"):
            lastletter="e"
        elif randomword.endswith("f"):
            lastletter="f"
        elif randomword.endswith("g"):
            lastletter="g"
        elif randomword.endswith("h"):
            lastletter="h"
        elif randomword.endswith("i"):
            lastletter="i"
        elif randomword.endswith("j"):
            lastletter="j"
        elif randomword.endswith("k"):
            lastletter="k"
        elif randomword.endswith("l"):
            lastletter="l"
        elif randomword.endswith("m"):
            lastletter="m"
        elif randomword.endswith("n"):
            lastletter="n"
        elif randomword.endswith("o"):
            lastletter="o"
        elif randomword.endswith("p"):
            lastletter="p"
        elif randomword.endswith("q"):
            lastletter="q"
        elif randomword.endswith("r"):
            lastletter="r"
        elif randomword.endswith("s"):
            lastletter="s"
        elif randomword.endswith("t"):
            lastletter="t"
        elif randomword.endswith("u"):
            lastletter="u"
        elif randomword.endswith("v"):
            lastletter="v"
        elif randomword.endswith("w"):
            lastletter="w"
        elif randomword.endswith("x"):
            lastletter="x"
        elif randomword.endswith("y"):
            lastletter="y"
        elif randomword.endswith("z"):
            lastletter="z"
        else:
            lastletter="I CODED SOMETHING WRONG!"
        if userguess != randomword:
            print "You guessed wrong..."
            print "\n"
            print "I'll give you a hint: the word STARTS with the letter " + firstletter + " and ENDS with the letter " + lastletter
            userguess=raw_input("I'll give you another chance to guess the word: ")
            if userguess != randomword:
                input = randomword
                output = ""
                for i in input.split():
                    output += i[1]
                secondletter=output
                print "You guessed wrong..."
                print "\n"
                print "I'll give you another hint: the second letter of the word is " + secondletter
                userguess=raw_input("I'll give you one last chance to guess the word: ")
                if userguess != randomword:
                    print "You guessed wrong..."
                    print "\n"
                    print "You're out of chances... the word was: " + randomword
                    print "I'm sorry to say this but..."
                    print "YOU LOST!"
                    print "\n"

                elif userguess == randomword:
                    print "You guessed right!"
                    print "YOU WON!"
                    print "\n"
                else:
                    print "I made a mistake in the code somewhere?!"
        elif userguess == randomword:
            print "You guessed right!"
            print "YOU WON!"
            print "/n"
        else:
            print "I made a mistake in the code somewhere?!"
        mindchange=raw_input("Wanna play again? If you do, just type 'Start'. If you don't then type 'Exit': ")
        if mindchange=="Exit":
            loop=False
        elif mindchange=="Start":
            loop=True
        else:
            print "You didn't type either 'Start' or 'Exit' : remember to include the Capitals!"
            print "\n"
    elif yesorno == "no":
        mindchange=raw_input("Well that's too bad, I was looking forward to beating you... well if you change your mind just type 'Start' but if you still don't want to play just type 'Exit':")
        if mindchange=="Exit":
            loop=False
        elif mindchange=="Start":
            loop=True
        else:
            print "You didn't type either 'no' or 'yes' : remember to not use capitals! "
            print "\n"
这是返回的内容:

Hello there! What might your name be? Alex
Hello Alex, would you like to play a friendly game of Guess the Word? Answer either 'yes' or 'no': yes
Ok then, here we go!


You have 3 guesses
I'm thinking of a word with 14 letters


_ _ _ _ _ _ _ _ _ _ _ _ _ _




Try and guess the word: SOMEWORD
You guessed wrong...


I'll give you a hint: the word STARTS with the letter o and ENDS with the letter g
I'll give you another chance to guess the word: SOMEOTHERWORD
You guessed wrong...


I'll give you another hint: the second letter of the word is v
I'll give you one last chance to guess the word: SOMEOTHERWORD
You guessed wrong...


You're out of chances... the word was: overregulating
I'm sorry to say this but...
YOU LOST!


Wanna play again? If you do, just type 'Start'. If you don't then type 'Exit': 
如果你也能指出我效率低下的地方,这可能也是无处不在的,那将不胜感激

WORDS = txt.splitlines()
WORDS_5_or_less = list(filter(lambda x: len(x) <= 5, WORDS))

然后只需从单词中选择“5”或“更少”而不是单词,使用列表理解

short_words = [word for word in txt.splitlines() if len(word) <= 5]

您需要使用一些数据结构,比如说字典和一些循环。这不是任何语言的编程方式,至少在python或ruby等语言中是如此。

此外,您可以摆脱重复的ifs块:

只需收到最后一封信:

    lastletter = randomword[-1]

    if not lastletter.isalpha():
        lastletter="I CODED SOMETHING WRONG!"
这同样适用于其他重复块:

    if lengthrandomword==2:
        print "_ _"
        print "\n"
    elif  lengthrandomword==3:
        print "_ _ _"
        print "\n"
    ...
    elif  lengthrandomword==15:
        print "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _"
        print "\n"
    else:
        print "I thought of a word and it's reeeaaaaaly long, you would never guess it, so I'm just gonna tell you that my word was " + randomword
您可以将其缩短为:

    if lengthrandomword in range(2,16):
        print(' '.join('_' * lengthrandomword))
        print('\n')
    else:
        print("I thought of a word and it's reeeaaaaaly long, you would never guess it, so I'm just gonna tell you that my word was " + randomword)

你在这里问了两个问题;请不要那样做。如何选择五个字符或更少的单词是一个很好的堆栈溢出问题。这是我的整个计划-我应该如何改进它?属于on。你可以删掉一个好的30 if语句。如果lengthrandomword==x,则替换。。。使用print*lengthrandomwordal,您可以只说lastletter=randomword[-1:]
    if lengthrandomword in range(2,16):
        print(' '.join('_' * lengthrandomword))
        print('\n')
    else:
        print("I thought of a word and it's reeeaaaaaly long, you would never guess it, so I'm just gonna tell you that my word was " + randomword)