刽子手帮助:无效的打印状态

2024-04-24 15:18:37 发布

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

我的刽子手代码在第40行有错误,第40行

print ('_'+ end=' ')

我的代码:

import time
import random
print('Lets play hangman')
print("""
       _______
       |    |
       |    O
       |    |
       |  \- -/
       |    |
       |   | |
       |  |   |
   ____|____
   """)



random_words = ['string', 'loop', 'python', 'print', 'run' , 'graphics window', 'variable', 'iteration', 'modules', 'module', 'input', 'logic', 'output    ']
time.sleep(2)
print ("What level would you like to play at? Too bad, you get random! Better type it in tho...")
level_of_difficulty = "Random"
time.sleep(2)
print ("The program is now generating your word...")
if level_of_difficulty == 'Random':
    generated_word = random.choice(random_words)

guessed = ''

lives = 7


while lives > 0:

    missed = 0
    print()
    for letter in generated_word:
        if letter in guessed:
            print("Congrats ya dumb animal, you got one")
        else:
            print ('_'+ end=' ')
            missed = missed + 1
    if missed == 0:
        print ('\n\nYou win!')  
        quit()
        break
    guess=input("Well what are you waiting for? guess a letter!:")

    guessed = guessed + guess
    if guess not in generated_word:
        print ('\n People like you are the reason why the human race will fail')
        print ('Fool')
    lives = lives - 1 
    missed = missed + 1

    print('your new lives value is ' + str(lives))

    if lives < 7:
        print('''   _______
   |    |   ''')
        if lives < 6:
            print('   |    O    ') 
            if lives < 5:            
                print('   |    |    ')
            if lives < 4:
                print('   |  \- -/  ')
            if lives < 3: 
                print('   |    |    ')
            if lives < 2:
                print('   |   | |  ')
            if lives < 1:
                print('   |  |   | ')
            if lives == 0:
             print('___|___      ')

             print('Game Over Boi')
             print('The secret word was ' + str(generated_word) + "fool")
             quit()

Tags: 代码inyouiftimerandomlevelgenerated
1条回答
网友
1楼 · 发布于 2024-04-24 15:18:37

so here is some code that partially belongs to me

这似乎是问题的根源。除了类型之外的主要问题是在使用Python2时使用Python3功能。你知道吗

print ('_'+ end=' ')

除了+需要是,的排版之外。在Python2中,end关键字不用于打印,它只在Python3中使用。相反,您可以在Python 2中执行以下操作:

print '_',

下一个问题是:

guess=input("Well what are you waiting for? guess a letter!:")

这将给您一个错误,因为在Python2中,它试图计算您作为表达式而不是字符串输入的内容。相反,您需要:

guess=raw_input("Well what are you waiting for? guess a letter!:")

使用这两个修复程序,您将有一个working game。尽管您可能希望添加一种方式来查看用户正确的字母。你知道吗

相关问题 更多 >