Python刽子手的破坏方式多于1

时间:2018-02-03 22:29:13

标签: python

我正在开发一个Python刽子手游戏。 我在这里和那里都有问题,并希望得到帮助。

1当你击败游戏时,这个词没有透露。 2邋code的代码 3输入后我无法显示该字母。正如您所看到的,程序中有很多调试。 (显示字母位置等) 你必须自己尝试这个程序才能看到它是如何工作的,因为我对它的构建方式毫无意义。

#! bin/python/

import random
import sys
import subprocess as sp

class bcolors:
    WARNING = '\033[93m'
    ENDC = '\033[0m'

words = ["animal", "random", "person", "chicken", "owl", "onion", "zebra"]
word =  random.choice(words)
empty = ""
letters=len(word)
letter2=letters-1
letterdashes = letters*["-", ]

guessremains=12
correct=0
used=""
correctletters=""
poslettercurrent = ""

#Begin
tmp = sp.call('clear',shell=True)
print bcolors.WARNING + "Hangman"
print
print bcolors.ENDC + "You are a prisoner who is going to be"
print bcolors.WARNING + "EXECUTED" + bcolors.ENDC + ". To prove you re innicent, you must"
print"solve a puzzle. After the set", guessremains, "guesses, you loose, and die."
print
print"However, if you win, you are set free."
print

posletter = ""
topline = ""
sectop = ""
thirtop = ""
fourtop = ""
fivetop = ""
sixtop = ""
secbottom = ""
bottom = ""
posletter = empty
letterdashes2 = ''.join(letterdashes)

#Main Guess Loop
while guessremains > 0:
    if guessremains < 12:
        bottom = "------------------"
    if guessremains < 11:
        secbottom = " /|\\"
    if guessremains < 10:
        fivetop = "  |         "
    if guessremains < 9:
        sixtop = "  |        "
    if guessremains < 8:
        fourtop = "  |         "
    if guessremains < 7:
        thirtop = "  |         "
    if guessremains < 6:
        sectop = " \|/        "
    if guessremains < 5:
        topline = "__________________"
    if guessremains < 4:
        sectop = " \|/        |"
        thirtop = "  |         |"
    if guessremains < 3:
        fourtop = "  |         O"
    if guessremains < 2:
        sixtop = "  |        /|\\"
        fivetop = "  |         /\\"
    print"==================================="
    print"word:"
    print letterdashes2
    print "letter positions:"
    print posletter
    print poslettercurrent
    print
    print"Used Letters:"
    print(used)
    print
    print"Correct Letters:"
    print(correctletters)
    print
    print(topline)
    print(sectop)
    print(thirtop)
    print(thirtop)
    print(fourtop)
    print(sixtop)
    print(fivetop)
    print(secbottom)
    print(bottom)
    input3 = raw_input("GUESS: ")
    input2 = word.count(input3)
    if input3 == "exit":
        sys.exit()
    if input3 == empty:
        sys.exit(bcolors.WARNING + "FATAL: char '' is not supported."  + bcolors.ENDC)
    if correct < letter2:
        if input3 in word:
            if input3 not in correctletters:
                if correctletters != empty:
                    correctletters = correctletters + ", " + input3
                else:
                    correctletters = input3
                print
                print("Correct!")
                print
                correct=correct+len(input3)
                if posletter == empty:
                    posletter =  word.find(input3)
                else:
                    posletter = posletter, word.find(input3)
                poslettercurrent =  word.find(input3)
                letterdashes2 = [w.replace(poslettercurrent, input3) for w in word]
            else:
                if input2 > 1:
                    print
                    print("Correct!")
                    print
                    correct=correct+len(input3)
                    poslettercurrent =  word.find(input3)
                    if posletter == empty:
                        posletter =  word.find(input3)
                    else:
                        posletter = posletter, word.find(input3)
                print"letter", input3, "already in use."
        else:
            if input3 not in used:
                if used != empty:
                    used = used + ", " + input3
                else:
                    used = input3
            print
            print("Wrong")
            print
            guessremains=guessremains-1
    else:
        letterdashes = [w.replace(input3, input3) for w in word]
        print
        print"Success!"
        print"You Win!"
        sys.exit()
    tmp = sp.call('clear',shell=True)
print
print bcolors.WARNING + "times up!"
print"you failed . . ." + bcolors.ENDC
print
print"you guessed", correct, "out of the", letters, "letters of the word", word

1 个答案:

答案 0 :(得分:0)

你把所有的印刷陈述都搞错了。

我已经为Python 3更正了

在python中打印时,它总是应该在parantheses print(ITEMS TO PRINT)

我还将raw_input改为input

我还在'print(word.upper())中添加了玩家获胜的时间。

最后一件事是将int转换为replace语句的字符串。 你可以在这里玩。

https://repl.it/@maffaz/WheatWrongChupacabra

import random
import sys
import subprocess as sp

class bcolors:
    WARNING = '\033[93m'
    ENDC = '\033[0m'

words = ["animal", "random", "person", "chicken", "owl", "onion", "zebra"]
word =  random.choice(words)
empty = ""
letters=len(word)
letter2=letters-1
letterdashes = letters*["-", ]

guessremains=12
correct=0
used=""
correctletters=""
poslettercurrent = ""

#Begin
tmp = sp.call('clear',shell=True)
print(bcolors.WARNING + "Hangman")
print()
print(bcolors.ENDC + "You are a prisoner who is going to be")
print(bcolors.WARNING + "EXECUTED" + bcolors.ENDC + ". To prove you re innicent, you must")
print("solve a puzzle. After the set", guessremains, "guesses, you loose, and die.")
print()
print("However, if you win, you are set free.")
print()

posletter = ""
topline = ""
sectop = ""
thirtop = ""
fourtop = ""
fivetop = ""
sixtop = ""
secbottom = ""
bottom = ""
posletter = empty
letterdashes2 = ''.join(letterdashes)

#Main Guess Loop
while guessremains > 0:
    if guessremains < 12:
        bottom = "------------------"
    if guessremains < 11:
        secbottom = " /|\\"
    if guessremains < 10:
        fivetop = "  |         "
    if guessremains < 9:
        sixtop = "  |        "
    if guessremains < 8:
        fourtop = "  |         "
    if guessremains < 7:
        thirtop = "  |         "
    if guessremains < 6:
        sectop = " \|/        "
    if guessremains < 5:
        topline = "__________________"
    if guessremains < 4:
        sectop = " \|/        |"
        thirtop = "  |         |"
    if guessremains < 3:
        fourtop = "  |         O"
    if guessremains < 2:
        sixtop = "  |        /|\\"
        fivetop = "  |         /\\"
    print("===================================")
    print("word:")
    print(letterdashes2)
    print("letter positions:")
    print(posletter)
    print(poslettercurrent)
    print()
    print("Used Letters:")
    print(used)
    print()
    print("Correct Letters:")
    print(correctletters)
    print()
    print(topline)
    print(sectop)
    print(thirtop)
    print(thirtop)
    print(fourtop)
    print(sixtop)
    print(fivetop)
    print(secbottom)
    print(bottom)
    input3 = input("GUESS: ")
    input2 = word.count(input3)
    if input3 == "exit":
        sys.exit()
    if input3 == empty:
        sys.exit(bcolors.WARNING + "FATAL: char '' is not supported."  + bcolors.ENDC)
    if correct < letter2:
        if input3 in word:
            if input3 not in correctletters:
                if correctletters != empty:
                    correctletters = correctletters + ", " + input3
                else:
                    correctletters = input3
                print()
                print("Correct!")
                print()
                correct = correct + len(input3)
                if posletter == empty:
                    posletter =  word.find(input3)
                else:
                    posletter = posletter, word.find(input3)
                poslettercurrent =  word.find(input3)
                letterdashes2 = [w.replace(str(poslettercurrent), input3) for w in word]
            else:
                if input2 > 1:
                    print()
                    print("Correct!")
                    print()
                    correct=correct+len(input3)
                    poslettercurrent =  word.find(input3)
                    if posletter == empty:
                        posletter =  word.find(input3)
                    else:
                        posletter = posletter, word.find(input3)
                print("letter", input3, "already in use.")
        else:
            if input3 not in used:
                if used != empty:
                    used = used + ", " + input3
                else:
                    used = input3
            print()
            print("Wrong")
            print(90)
            guessremains=guessremains-1
    else:
        letterdashes = [w.replace(input3, input3) for w in word]
        print(90)
        print("Success!")
        print("You Win!")
        print(word.upper())
        sys.exit()
    tmp = sp.call('clear',shell=True)
print()
print( bcolors.WARNING + "times up!")
print("you failed . . ." + bcolors.ENDC)
print()
print("you guessed", correct, "out of the", letters, "letters of the word", word)