我需要帮助在python hangman gam中打印列表

2024-03-29 10:38:40 发布

您现在位置:Python中文网/ 问答频道 /正文

在第24行,我一直得到一个语法错误,我不知道为什么,我想打印列表,并使列表中的单词添加到hangman游戏我明白元组是不变的,所以这就是为什么我不使用它们。如果有人能帮我纠正这个错误,我将不胜感激。你知道吗

import random
import stickman


MAX_WRONG = len(stickman.HANGMAN) - 1
MAX_TRYINGS = 7


WORDS = {
    'forest':('ANT', 'BABOON', 'BADGER', 'BAT', 'BEAR'),
    'desert':('CAMEL', 'COYOTE', 'DONKEY', 'LIZARD', 'HAWK'),
    'swamp':('FROG', 'PYTHON', 'DUCK', 'FISH', 'CROCODILE'),
    'other':('GOAT', 'GOOSE', 'MOLE', 'MOUSE', 'MONKEY')
    }


EXPLANATIONS = (
    ('There once was an animal that lived in the wild it was an', print['forest'],
     'there once was an animal that lived in a desert and it was an',print['desert'],
     'there once was an animal that lived in the swamp and it was an',print['swamp'],
     'there once was an animal that lived all around the world and it was an',print['other'])


HEADER = ('-', 'Hangman Game', 'by Mohammad AlMutairi', '-', '')

INSTRUCTION = ('A certain word has been jumbled,',
               'you are to guess the word that is jumbled', '',
               'In this game you must guess a word by a number ',
               'of finite guesses before being hanged.', '',
               'Good luck!', '')

#menu that explains the objective of the game and is for aesthetic appeal 
for line in HEADER:
    if line:
        print('{:-^60}'.format(line))
    else:
        print('\n')
#asks the user for input on their name and explains the objective of the game
name = input('Hello there! What is your name? ')
print('Welcome {}, to my python hangman game!'.format(name.capitalize()))
for line in INSTRUCTION:
    if line:
        print('{:^60}'.format(line))
    else:
        print('\n')

#menu before starting the game telling the user what to do.
print('Menu\n')
while True:
    for menu_number, group in enumerate(WORDS):
        print('{:2d} - {} animals'.format(menu_number + 1, group[0].capitalize()))
    print(' 0 - Exit')

    #Gets input from the user by what number they have chosen, than gives them hints
    try:
        selection = int(input('Your selection: '))
    except ValueError:
        print('Please enter a number, not a sign!')
        continue
    if selection == 0:
        print('Bye!\n')
        exit()
    elif 0 < selection <= len(WORDS):
        selection -= 1
        break
    print('That is not one of your choices.')

print(EXPLANATIONS[selection].format(random.choice(WORDS[selection][1])))
word = random.choice(WORDS[selection - 1][1])

MAX_WRONG = len(stickman.HANGMAN) - 1

WORDS=['forest','desert','swamp','other']

# initialize variables
word = random.choice(WORDS) # the word to be guessed

so_far = "-" * len(word) # one dash for each letter in word to be guessed

wrong = 0 # number of wrong guesses player has made

used = [] # letters already guessed

print("Welcome to Hangman. Good luck!")
while wrong < MAX_WRONG and so_far != word:
    print(stickman.HANGMAN[wrong])
    print("\nYou've used the following letters:\n", used)
    print("\nSo far, the word is:\n", so_far)

    guess = input("\n\nEnter your guess: ")
    guess = guess.upper()
    while guess in used:
        print("You've already guessed the letter", guess)
        guess = input("Enter your guess: ")
        guess = guess.upper()
    used.append(guess)

    if guess in word:
        print("\nYes!", guess, "is in the word!")
        print("Good job!",name.capitalize(),".")
    # create a new so_far to include guess
        new = ""

        for i in range(len(word)):
            if guess == word[i]:
               new += guess   
            else:
                new += so_far[i]
        so_far = new

    else:
        print("\nSorry,", guess, "isn't in the word.")
        wrong += 1

if wrong == MAX_WRONG:
    print(stickman.HANGMAN[wrong])
    print("\nSorry",name.capitalize(),"you have been hanged.")
else:
    print("\nYou guessed it!")


print("\nThe word was", word)
print('''@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@'        .@@@@@@@@+      #@@@@@+      @@@@       @@@            '@@@@@@@@@@@@.       ,@@@@      @@@@@      @.            @@@           ,@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@#` `         #@@@@@@        @@@@@+      +@@@       @@@            '@@@@@@@@@@#           @@@;     @@@@#`   `+@`            @@@             @@@@@@@@@@@@@@@@
@@@@@@@@@@@#,            ,@@@@@@        @@@@@+       @#@       @@@            '@@@@@@@@@@             #@@     '@@@;     @@.            @@@             '@@@@@@@@@@@@@@@
@@@@@@@@@@@@      .`      @@@@@+        #@@@@+       @@`       @@@            '@@@@@@@@@`      +      ,@@      #@@     .@@.            @@@              @@@@@@@@@@@@@@@
@@@@@@@@@@@.     #@@;    #@@@@@          @@@@+       #@        @@@     @@@@@@@@@@@@@@@@@     ,@@@      @@'`    @@@`    #@@.    `@@@@@@@@@@     @@@`     @@@@@@@@@@@@@@@
@@@@@@@@@@@     `@@@@@@@@@@@@@@          @@@@+       `@        @@@            @@@@@@@@@+    `@@@@@     @@@     @@#     @@@.            @@@     @@@      @@@@@@@@@@@@@@@
@@@@@@@@@@@     ;@@     ` #@@@+    ``    #@@@+        ;   `    @@@            @@@@@@@@@+     @@@@#     @@@.     @     :@@@.            @@@        `    #@@@@@@@@@@@@@@@
@@@@@@@@@@@     @@@       #@@@     ';     @@@+            #    @@@            @@@@@@@@@+     @@@@@     @@@@     @     @@@@.            @@@            :@@@@@@@@@@@@@@@@
@@@@@@@@@@@     '@@       #@@@     @@     @@@+    .       #    @@@            @@@@@@@@@+     @@@@@     @@@@     @     @@@@.            #@@           @@@@@@@@@@@@@@@@@@
@@@@@@@@@@@     `@@       #@@:      `     ;@@+    @      `#    @@@     @@@@@@@#@@@@@@@@+     @@@@@     @@@@:         ;@@@@.    `@@@@@@@@@@     :`     @@@@@@@@@@@@@@@@@
@@@@@@@@@@@`     @@@@@    #@@              @@+    @      '+    @@@     @@@@@@@@@@@@@@@@@     '@@@`     @@@@@         @@@@@.    `@@@@@@@@@@     #:`    .@@@@@@@@@@@@@@@@
@@@@@@@@@@@@      ;#,     #@#              #@+    @`     @#    @@@     ;;;;;;;:@@@@@@@@@      :@,`    .@@@@#`       .@@@@@.    `;;;;;;;#@@     @@     `@@@@@@@@@@@@@@@@
@@@@@@@@@@@@`             #@`              .@+    @;     @+    @@@             @@@@@@@@@#`            @@@@@@#       #@@@@@.            +@@     @@+      @@@@@@@@@@@@@@@
@@@@@@@@@@@#@            `@@     `#@@@`     @+    @@    .@+    @@@             @@@@@@@@@@;`          #@@@@@@@       @@@@@@.            +@@     @@#      @@@@@@@@@@@@@@@
@@@@@@@@@@@@@@,`        #@@@`    ;@@@@:     @+  ` @@    @@#    @@@             #@@@@@@@@@@#       ` #@@@@@@@@`     ,@@@@@@.            +@@     @@@#      @@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@++++++@@@@@@@@@@##@@@@#@@@@@@@@@@@@@@@@@#@@@@@@@@@@@@@@@@@@@@@@#@@@@@@@@@@@@@+++++@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''')

Tags: andthetoinanforthatword
2条回答
EXPLANATIONS = (
    ('There once was an animal that lived in the wild it was an', print['forest'],
     'there once was an animal that lived in a desert and it was an',print['desert'],
     'there once was an animal that lived in the swamp and it was an',print['swamp'],
     'there once was an animal that lived all around the world and it was an',print['other']
    )
)

这不是有效的python代码。print['...']?这该怎么办?如果写对了,你想得到什么?你知道吗

>>> example = (print('forest'), print('desert'), print('swamp'))
forest
desert
swamp
>>> print(example)
(None, None, None)

这里缺少一个端括号-

EXPLANATIONS = (
    ('There once was an animal that lived in the wild it was an', print['forest'],
     'there once was an animal that lived in a desert and it was an',print['desert'],
     'there once was an animal that lived in the swamp and it was an',print['swamp'],
     'there once was an animal that lived all around the world and it was an',print['other'])

关上它

EXPLANATIONS = (
    ('There once was an animal that lived in the wild it was an', print['forest'],
     'there once was an animal that lived in a desert and it was an',print['desert'],
     'there once was an animal that lived in the swamp and it was an',print['swamp'],
     'there once was an animal that lived all around the world and it was an',print['other']
    )
)

相关问题 更多 >