用raw_input替换[]

时间:2013-07-31 03:36:59

标签: python replace

我有什么

import random

listofLetters =['k', 'i', 'n', 'g']
x = random.choice(listofLetters)
print x 

listDash = []

print "Welcome to Gues the Letter Game!"
print " "
print "=" * 5 + "Start of Game" + "=" * 5 
print ['_', '_', '_', '_', '_']
print " "

maxLife = 4

print "*" * 3 + "Life Line" + "*" * 3
print "-+" * maxLife
print "$|" * maxLife
print "-+" * maxLife
print " " 


guess = raw_input("Enter your guess: ")
howmanyletter = len(guess)

if guess == x:

    print "*" * 3 + "Life Line" + "*" * 3
    print "-+" * maxLife
    print "$|" * maxLife
    print "-+" * maxLife

else:
    print "Incorrect Guess []

我希望[]替换为guess

例如[k]

3 个答案:

答案 0 :(得分:3)

以下是有关Python string.format method的一些信息。

x=raw_input("guess? ")
print "Incorrect guess [{}].".format(x)

答案 1 :(得分:0)

print "Incorrect Guess [%s]" % guess

OR

print "Incorrect Guess [" + guess + "]"

答案 2 :(得分:0)

raw_input()函数返回一个字符串,所以你只需要在print语句中添加“guess”:

print "Incorrect Guess [", guess, "]"