首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >新Hangman Python

新Hangman Python
EN

Stack Overflow用户
提问于 2012-11-13 11:47:00
回答 1查看 720关注 0票数 0

我正在开发一个Hangman游戏,但我在用猜测的字母替换破折号时遇到了问题。新字符串只是添加新的破折号,而不是用猜测的字母替换破折号。

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

代码语言:javascript
复制
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: ")
EN

回答 1

Stack Overflow用户

发布于 2012-11-13 12:02:03

下面的代码块似乎有问题:

代码语言:javascript
复制
elif (formula2[i] == guess):
    new[i] = guess
else:
    new[i] =new + "-"

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

代码语言:javascript
复制
elif formula2[i] == guess:
    new += guess
else:
    new += '-'

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13355316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档