所以我不熟悉编码,作为课程的一部分,我正在制作一个刽子手游戏。当我运行该程序时,它会显示“['','','','','','',' ',''] 输入:“。我想这样做,以便不打印撇号和逗号。我觉得.join()函数是最好的方法,但是当我还在学习时,我不知道如何和/或在哪里放置功能。请耐心等待!谢谢!!
original=list(word)
temp=list(word)
guess=[] #null list
lettersguessed=[]
trial=int(0) #for keeping track of guessess
userinput=''
counter=int(0) #keeping track of position of element in list (if found)
letterlist=['abcdefghijklmnopqrstuvwxyz']
for i in range(len(original)): #creating the '_ _**....' list
if (original[i]==' '):
guess.append(" ") #(whitespace) for vowels
else:
guess.append("_") #_ for all other alphabets
答案 0 :(得分:1)
我想我得到了你想要的东西。 我只是将使用过的字母的if语句移到了你的top的顶部,而不是附加到猜测变量的字母,我附加了userinput变量。
print "WELCOME TO HANGMAN!"
print ' '
print '------------------------------------------'
print ' '
import random #for random.choice
dictionary=['CODING', 'GENEPEEKS', 'PYTHON', 'FREE TOM BRADY', 'BOSTON']
word=random.choice(dictionary)
original=list(word)
temp=list(word)
guess=[] #null list
lettersguessed=[]
trial=int(0) #for keeping track of guessess
userinput=''
counter=int(0) #keeping track of position of element in list (if found)
for i in range(len(original)): #creating the '_ _**....' list
if (original[i]==' '):
guess.append(" ") #(whitespace) for vowels
else:
guess.append("_") #_ for all other alphabets
print guess
while trial<15:
userinput=str.upper(raw_input('Input : '))
if userinput in lettersguessed: # test presence
print "This letter has been used already!"
continue
else:
lettersguessed.append(userinput) # remember as used
if len(userinput)>1: #checking for multiple characters
print 'Error : Input only a single character'
trial -= 1
continue
if userinput in original:
while userinput in temp: #loop for checking redundant characters
counter=temp.index(userinput)
guess[counter]=userinput
temp.remove(userinput)
temp.insert(counter,'_')
counter=0
for i in range(0,len(temp)): #checking for final guess match with original
if temp[i]=='_':
counter+=1
if counter==len(original): #if guess matches original
print 'Correct\t', guess
print 'You Win !'
trial=10
break
print 'Correct\t' , guess , '\tTrials left: ', (9-trial)
else:
trial+=1
print 'Incorrect', '\tTrials left: ', (9-trial)
else:
print 'You Lose !'
print 'Correct answer was\t', original
答案 1 :(得分:0)
在开头添加:
from string import ascii_uppercase
options = set(ascii_uppercase)
并在输入一个图表检查后添加检查允许的字母:
if userinput not in options:
print 'Error: you already chose -', userinput
continue
else:
options.remove(userinput)