使用Python

时间:2017-10-27 17:35:55

标签: python

我需要帮助在这个我正在上学的刽子手游戏小项目中创建子级别。有人已经帮我这个,但现在我的老师也想要子级别。但对于那些次级,他想要一组不同的词。所以基本上下面的单词是为了简单,中等和艰难。然后子级别很容易1,简单2和容易3.同样适用于中等,然后相同适用于硬。我已经做过简单1,中等1和硬1.剩下的子级别是2和3.任何人都可以帮忙吗?希望这是有道理的。非常感谢你。

import time
import random

#words

hardWords = ['triskaidekaphobia', 'spaghettification', 'sesquipedalian', 'floccinaucinihilipilification', 'deipnosophist']
medWords = ['erubescent', 'entomophogy', 'noctambulist', 'parapente', 'umbriferous']
simpWords = ['cat', 'house', 'degust', 'glaikit', 'otalgia']

# Just use one word, which will be set after user selects difficulty
#simpWordsR = random.choice(simpWords)
#medWordsR = random.choice(medWords)
#hardWordsR = random.choice(hardWords)


#welcome the user

name = input("What is your name?")
print ("Hello! " + name + ". Time to play Hangman")

#wait for 1 second
time.sleep(1)

print ("")

# Removed uppercase. We will only use lower case
alphabet = 'abcdefghijklmnopqrstuvwxyz'

#picking levels

gameMode = input('Choose a level - Easy (10 Guesses), Medium (8 Guesses) or Hard (7 Guesses)').lower()
if gameMode == 'easy':
    numberOfGuesses = 11
    theWord = random.choice(simpWords)
    # Not sure what this is for....deleted
    #correctLetters = ''
    #for i in range(len(simpWordsR)): # replace blanks with correctly guessed letters
    #  if simpWordsR[i] in correctLetters:
    #    blanks = blanks[:i] + simpWordsR[i] + blanks[i+1:]
    #print (blanks)

elif gameMode == 'medium':
    numberOfGuesses = 8
    theWord = random.choice(medWords)

else:
    numberOfGuesses = 7
    theWord = random.choice(hardWords)

print(gameMode)
print(theWord)  # For debugging purposes

# Since python strings are immutable, use a list
blanks = ['_'] * len(theWord)

# Need to keep a list of already guessed letters
used_letters = []

time.sleep(1)

print ("")

# Move this to the loop
#numberOfGuesses1 -= 1
#print (numberOfGuesses1)

#while numberOfGuesses1 == 10:
while numberOfGuesses > 0:
    print (numberOfGuesses)
    print (' '.join(blanks))

    # get user input and convert to lower case
    guess = input("Guess a Character!").lower()

    # Make sure it's a letter
    if not guess in alphabet:
        print("Enter a letter...")
    # Make sure not already guessed
    elif guess in used_letters:
        print("Already guessed that....")
    # Valid guess. Check it
    else:
        used_letters.append(guess)
        if (guess in theWord):
            print ("Well Done! You Guessed it right!")
            # Loop through and replace
            for x in range(0, len(theWord)):
                if theWord[x] == guess:
                    # Note: this works since theWord and blanks have the same length
                    blanks[x] = guess
            # Check for success
            if not '_' in blanks:
                print("You win")
                # End the loop
                break
        else:
            print ("The letter is not in the word. Try Again!")
            # Only decrement if incorrect
            numberOfGuesses -= 1

print ("Game Finished. Maybe Try Again y/n.")

1 个答案:

答案 0 :(得分:0)

这不是要求人们为你做功课的地方。那说,你有几个选择。

  1. 就像您创建列表一样,只需创建更多列表和更多elif分支。

  2. 更好的选择是将逻辑块合并为可重用的方法。 这将清理您的代码,并允许您根据需要在以后的更改中更加通用。

  3. 更好的方法和变量合并到类中,这将使这个游戏更容易实现。

  4. 尽管如此,在这种情况下,“快速”选项是第一个选项。