选择的猜字游戏单词

0 投票
1 回答
669 浏览
提问于 2025-04-16 17:32

我正在尝试让用户选择他们想要的单词长度,但我遇到了一些困难,不知道哪里出了问题。我刚学了三个月的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行,
secretword = getrandomword(word)

类型错误:getrandomword() 不接受任何参数(给了1个)

1 个回答

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

可能还有其他问题,但这些是最明显的,需要修正的地方。

撰写回答