Python 新刽子手蟒蛇

Python 新刽子手蟒蛇,python,while-loop,Python,While Loop,我正在做一个刽子手游戏,但是我很难用猜出来的字母替换破折号。新字符串只是添加新的破折号,而不是用猜测的字母替换破折号 如果有人能帮忙,我将不胜感激 import random import math import os game = 0 points = 4 original = ["++12345","+*2222","*+33333","**444"] plusortimes = ["+","*"] numbers = ["1","2","3"] #FUNCTIONS def

我正在做一个刽子手游戏,但是我很难用猜出来的字母替换破折号。新字符串只是添加新的破折号,而不是用猜测的字母替换破折号

如果有人能帮忙,我将不胜感激

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: ")
随机导入
输入数学
导入操作系统
游戏=0
点数=4
原件=[“++12345”、“+*2222”、“*+33333”、“**444”]
加总次数=[“+”,“*”]
数字=[“1”、“2”、“3”]
#功能
def firstPart():
打印“欢迎来到数字刽子手游戏!”
def示例():
result=“”
ori=随机选择(原始)
对于范围(2,len(ori))中的i:
如果i%2==0:
结果=结果+ori[i]+ori[0]
其他:
结果=结果+ori[i]+ori[1]
返回ori
#def实际值名称(长度):
#顶级
第一部分()
播放=原始输入(“您想播放吗?Y-是,N-否:”)
而(播放=“Y”和(点数>=2)):
游戏=游戏+1
点数=点数
打印“正在玩游戏:”,游戏
打印“到目前为止您的积分是:”,积分
limit=输入(“您希望允许的最大错误猜测数?”)
长度=输入(“公式(包括符号)所需的最大长度”(必须大于等于5)?)
结果=“跟踪”
ori=随机选择(原始)
对于范围(2,len(ori))中的i:
如果i%2==0:
结果=结果+ori[i]+ori[0]
其他:
结果=结果+ori[i]+ori[1]
测试=评估(结果[:-1])
v=随机选择(加上次数)#随机生成公式的开始
va=随机选择(加数倍)
公式=“”

而(len(formula)以下块似乎有问题:

elif (formula2[i] == guess):
    new[i] = guess
else:
    new[i] =new + "-"
Python不允许修改字符串中的字符,因为它们是不可变的(无法更改)。请尝试将所需的字符附加到
新的
字符串中。例如:

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

最后,你应该把
new
的定义放在循环的正下方,因为你想在每次猜测后重新生成它。

我看不出你在代码中试图做一些你不能做的事情。如果你想用不同的字符替换字符串中的字符,看看你在l下面的意思是什么哦,你是说while循环吗?我用new+=guess和new+=“-”运行了它,但什么都没发生。程序只是在while循环中运行。程序是否以错误结束?