简体   繁体   English

Python:Hangman游戏问题

[英]Python: Hangman game problems

I am brand new to Python but a have a littel Matlab and C++ background. 我是Python的新手,但是有Matlab和C ++的背景。 Please Help!!!! 请帮忙!!!!

I am having problems with my hangman code. 我的子手代码有问题。 If a word has multiple of the same letter in I cannot figure out how to get it to switch all of them. 如果一个单词中有多个相同的字母,我将无法弄清楚如何切换所有单词。 I have a couple tries with some of them commented out. 我有几次尝试,其中一些被注释掉了。

import random
import time
import sys

def pickWord():
    words = [line.strip() for line in open('word_list.txt')]
    word = random.choice(words)
    word = word.lower()
    return word

def checkLetter(word, input):
    if input not in word:
        in_letter = 0
    else:
        in_letter = 1
    return in_letter

def check_input(input):
    if input.isaplha() == False :
        input = raw_input('Your input was not a letter, please enter a letter: ')
    elif len(input) > 0:
        input = raw_input('Your entry was longer than 1 letter, please enter     one letter: ')
    else:
        input = input
    return input



#main function

running = 'y'

print('Lets Play Hangman!\n\n ------------------------ \n\nGenerating a Random   word \n\n')

while running == 'y':

word = pickWord()
letters = list(word)

time.sleep(3)

print ('The word has been chosen\n\n')

print '%s' % word

start = raw_input('Are you ready to start?(y/n)')
start = start.lower()

if start == 'n':
    print('Well, too damn bad. Here We go!\n\n **************************\n\n')
elif start == 'y':
    print('Awesome, lets begin!\n\n*********************************\n\n')
else:
    print('You did not enter y or n, sorry you are not allowed to play!')
    sys.exit()

i = 0

print ('The word has %d letters in it') % len(word)

input = raw_input('What is your first guess: ')
input = input.lower()
correct = ['_'] * len(word)

print ' '.join(correct)

while correct != letters and i <= 5:
    '''if input in letters:
        for input in letters:
            location = letters.index(input)
            correct[location] = input
        print('You guessed the correct letter! Your input %s is in the word at the %d spot.') % (input, location)
        print ' '.join(correct)
    elif input not in letters:
        print('You guessed wrong :(')
        i = i + 1
        guesses = 6 - i
        print('You have %d guesses left.') % guesses
        guesses = 0
    else:
        print('You did not enter a valid letter, please try again.')'''

    '''for j in letters:
        if j == input:
            location = letters.index(j)
            correct[location] = j
            print '%s' % ' '.join(correct)
            print '%d' % location
            print '%s' % j
        if j == input:
            location = letters.index(j)
            correct[location] = j
        print('You guessed the correct letter! Your input %s is in the word at the %d spot.') % (input, location)
        print ' '.join(correct)'''

    if input not in letters:
        i = i + 1 
        guesses = 6 - i
        print("You guessed incorrectly. You have %d guesses left.") % guesses


    input = raw_input('What is your next guess: ')
    input = input.lower()

if correct == letters:
    print('Congrats! You won the game!')
else:
    print('You lost. :(')

running = raw_input('Do you want to play again? (y/n)').lower()

In your attempt, the loop is stopping when it finds the first match of input to letters. 在您的尝试中,当找到输入与字母的第一个匹配项时,循环正在停止。

the following code will work: 以下代码将起作用:

guess = raw_input('What is your first guess: ')
word = "wordword"
letters = list(word)
correct = ['_']* len(word)
for x, y in enumerate(word):
    if guess == y:
        correct[x] = y

Your mistakes 你的错误

In your first attempt: 第一次尝试:

if input in letters:
    for input in letters:

you are checking if input is in letters, which is fine, but if this returns True, inputs original value is lost, and is reassigned as you loop through the elements of letters . 要检查,如果输入的是字母,这是很好的,但如果返回true,输入原始值丢失,并通过的元素重新分配,你循环letters

eg 例如

>>>input = "w"
>>>word = "word"
>>>if input in word:
...    for input in word:
...        print(input)

w
o
r
d

your second attempt 你的第二次尝试

for j in letters:
    if j == input:
        location = letters.index(j)

is a lot closer to being successful, however location = letters.index(j) is always going to equal the index of the first match of j, and thus will not assign all matched values of input. 比成功更接近成功,但是location = letters.index(j)总是等于j的第一个匹配项的索引,因此不会分配输入的所有匹配值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM