刽子手一词被选中

时间:2011-05-12 22:29:31

标签: python graphics

我试图让用户选择他们想要的单词的长度,但是我很难弄清楚出了什么问题。我是Python的菜鸟,我学了3个月,我搜索过Python。我不知道所有Python内置的函数,或者我可能知道,但不知道如何使用它们。

这是我的代码:

from random import *
import os


wordlist_1 = 'cat dog'.split()                #Is all the wordlist be in a function to all three of wordlist?
wordlist_2 = 'donkey monkey dragon'.split()   #Like def wordlist():
wordlist_3 = 'dinosaur alligator buffalo'.split()

keep_playing = True
def print_game_rules(max_incorrect,word_len):
    print"You have only 7 chances to guess the right answer"
    return

def length_1():
    print('You will have length of 3 word')
    return
def length_2():
    print('You will have length of 6 word')
    return
def length_3():
    print('You will have length of 8 word')
    return

def Welcomenote():

    print('Select One Category')
    print(' 1: length of 3 word')
    print(' 2: length of 6 word')
    print(' 3: length of 8 word') 

    choice = {
        "1": length_1,
        "2": length_2,
        "3": length_3 }

    choose = input()
    return

def getrandomword():
    wordindex = random.randint(0,len(wordlist)-1)
    return wordlist[wordindex]

def get_letter():
    print
    letter = raw_input("Guess a letter in the mystery word:") 
    letter = letter.strip()
    letter = letter.lower()
    print
    os.system('cls')
    return letter
def display_figure(hangman):
    graphics = [
    """
         +-------+
         |
         |
         |
         |
         |
         |
     ====================
    """,
    """
         +-------+
         |       |
         |
         |
         |
         |
         |
     ====================
    """,
    """
        +-------+
        |       |
        |       O
        |
        |
        |
        |
    ====================
    """,
    """
        +-------+
        |       |
        |       O
        |       | 
        |
        |
        |
    =====================
    """,
    """
        +-------+
        |       |
        |       O
        |      /| 
        |
        |
        |
    =====================
    """,
    """
        +-------+
        |       |
        |       O
        |      /|\               
        |
        |
        |
    =====================
    """,
    """
        +-------+
        |       |
        |       O
        |      /|\               
        |      /  
        |
        |
    =====================
    """,
    """
        +-------+
        |       |
        |       O
        |      /|\               
        |      / \               
        |
        |
    =====================
    """]

    print graphics[hangman]
    return

while keep_playing:
    word=Welcomenote()
    secretword = getrandomword(word)
    guesses='_' * len(secretword)
    max_incorrect=7
    alphabet="abcdefghijklmnopqrstuvxyz"
    letters_tried=""
    number_guesses=0
    letters_correct=0
    incorrect_guesses=0
    print_game_rules(max_incorrect,word_len)


    while (incorrect_guesses != max_incorrect) and (letters_correct != secretword):
        letter=get_letter()
        display_figure(incorrect_guesses)
        if len(letter)==1 and letter.isalpha():
            if letters_tried.find(letter) != -1:
                print "You already picked", letter
            else:
                letters_tried = letters_tried + letter
                first_index=word.find(letter)
                if  first_index == -1:
                    incorrect_guesses= incorrect_guesses +1
                    print "The",letter,"is not the mystery word."
                else:
                    print"The",letter,"is in the mystery word."
                    letters_correct=letters_correct+1
                    for i in range(len(secretword)):
                        if letter == secretword[i]:
                            guesses[i] = letter

        else:
            print "Please guess a single letter in the alphabet."

        print ' '.join(guesses)
        print "Letters tried so far: ", letters_tried
        if incorrect_guesses == max_incorrect:
            print "Sorry, too many incorrect guesses. You are hanged."
            print "The word was",word
            keep_playing = False
        if letters_correct == secretword:
            print "You guessed all the letters in the word!"
            print "The word was",word
            keep_playing = False


    if keep_playing == False:
        user = raw_input("\n\tShall we play another game? [y|n] ")
        again = "yes".startswith(user.strip().lower())
        if again:
            keep_playing = True
        else:
            break

raw_input ("\n\n\nPress enter to exit")

现在错误说:

  

追踪(最近的呼叫最后):
  档案“P:\ Lesson 8 \ Hangman   2 - Copy.py“,第156行,in       secretword = getrandomword(word)

     

TypeError:getrandomword()不需要   参数(给出1个)

1 个答案:

答案 0 :(得分:5)

要解决有关randint不存在的错误,您已在random模块中导入了函数名称。

from random import *

所以直接调用该函数。

wordindex = randint(0,len(wordlist)-1)

所以你输入的数量不是你所需要的,我建议你直接导入模块(所以你不需要更改函数调用)或单个名称(因此你不会污染全局命名空间)与你不使用的其他随机函数。)

import random
# or
from random import randint

但是您的代码中还存在其他一些问题。

变量length_1length_2length_3分配了字符串。之后,您将它们重新定义为函数。在这些函数中,您将返回那些函数名称(而不是那些字符串)。稍后在Welcomenote()中,您将返回这些函数(而不是那些字符串),因此它是错误的。你应该使用不同的名字。

此外,看起来你的意思是他们是你的单词列表。如果保持原样,您将获得单独的字母。它应该是一个列表。你应该定义它们。

wordlist_1 = ['cat', 'dog']
# or alternatively
wordlist_1 = 'cat dog'.split() # splits the string up to a list of words separated by whitespace

可能有其他人,但这些是最突出的,应该修复的。