0

ハングマン ゲームに取り組んでいますが、ダッシュを推測された文字に置き換えるのに問題があります。新しい文字列は、ダッシュを推測された文字に置き換えるのではなく、新しいダッシュを追加するだけです。

誰かが助けてくれれば本当にありがたいです。

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: ")
4

1 に答える 1

1

次のブロックは問題があるようです。

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

Pythonでは、文字列は不変であるため(変更できないため)、文字列内の文字を変更することはできません。new代わりに、目的の文字を文字列に追加してみてください。例えば:

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

最後に、new推測のたびにループを再生成するため、ループ内の定義を真下に配置する必要があります。

于 2012-11-13T04:02:03.030 に答える