新的Hangman Python

时间:2012-11-13 03:47:00

标签: python while-loop

我正在制作一个Hangman游戏,但是我无法用猜到的字母替换破折号。新字符串只是添加了新的破折号而不是用猜测的字母替换破折号。

如果有人能提供帮助,我将非常感激。

import random
import math
import os

game = 0
points = 4

original = ["++12345","+*2222","*+33333","**444"]
plusortimes = ["+","*"]
numbers = ["1","2","3"]




#FUNCTIONS

def firstPart():
    print "Welcome to the Numeric-Hangman game!"

def example():
        result = ""
        ori = random.choice(original)
        for i in range(2,len(ori)):
                if i % 2 == 0:
                        result = result + ori[i] + ori[0]
                else:
                        result = result + ori[i] + ori[1]
        return ori



# def actualGame(length):







#TOP LEVEL





firstPart()

play = raw_input("Do you want to play ? Y - yes, N - no: ")

while (play == "Y" and (points >= 2)):
    game = game + 1
    points = points
    print "Playing game #: ",game
    print "Your points so far are: ",points
    limit = input("Maximum wrong guesses you want to have allowed? ")
    length = input("Maximum length you want for the formulas (including symbols) (must be >= 5)? ")

    result = ""                                           #TRACE
    ori = random.choice(original)
    for i in range(2,len(ori)):
          if i % 2 == 0:
              result = result + ori[i] + ori[0]
          else:
               result = result + ori[i] + ori[1]   
    test = eval(result[:-1])

    v = random.choice(plusortimes)                         #start of randomly generated formula
    va = random.choice(plusortimes)
    formula = ""
    while (len(formula) <= (length - 3)):
        formula = formula + random.choice(numbers)
        formula2 = str(v + va + formula)


    kind = ""
    for i in range(2,len(formula2)):
            if i % 2  == 0:
                kind = kind + formula2[i] + formula2[0]
            else:
                kind = kind + formula2[i] + formula2[1]

    formula3 = eval(kind[:-1])

    partial_fmla = "------"

    print "     (JUST TO TRACE, the program invented the formula: )" ,ori
    print "     (JUST TO TRACE, the program evaluated the formula: )",test
    print "The formula you will have to guess has",length,"symbols: ",partial_fmla
    print "You can use digits 1 to 3 and symbols + *"


    guess = raw_input("Please enter an operation symbol or digit: ")



    a = 0
    new = ""
    while a<limit:
        for i in range(len(formula2)):
                if (formula2[i] == partial_fmla[i]):
                    new =  new + partial_fmla[i]


                elif (formula2[i] == guess):
                    new[i] = guess


                else:
                    new[i] =new + "-"


        a = a+1           
        print new

    guess = raw_input("Please enter an operation symbol or digit: ")







    play = raw_input("Do you want to play ? Y - yes, N - no: ")

1 个答案:

答案 0 :(得分:1)

以下街区似乎有问题:

elif (formula2[i] == guess):
    new[i] = guess
else:
    new[i] =new + "-"

Python不允许修改字符串中的字符,因为它们是不可变的(无法更改)。请尝试将所需字符添加到new字符串中。例如:

elif formula2[i] == guess:
    new += guess
else:
    new += '-'

最后,您应该将new的定义直接放在循环中,因为您希望在每次猜测后重新生成它。