Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用.join()函数的正确方法是什么?_Python_Function_Join - Fatal编程技术网

Python 使用.join()函数的正确方法是什么?

Python 使用.join()函数的正确方法是什么?,python,function,join,Python,Function,Join,所以我对编码是新手,作为我课程的一部分,我正在制作一个刽子手游戏。当我运行程序时,它会显示“[”,“,”,“,”,“,”,“,”,”] 输入:“。我想使撇号和逗号不被打印出来。我觉得.join()函数是实现这一点的最佳方法,但由于我仍在学习,我不确定如何和/或在何处放置该函数。请容忍我!谢谢 original=list(word) temp=list(word) guess=[] #null list lettersguessed=[] trial=int(0) #for keeping tra

所以我对编码是新手,作为我课程的一部分,我正在制作一个刽子手游戏。当我运行程序时,它会显示“[”,“,”,“,”,“,”,“,”,”] 输入:“。我想使撇号和逗号不被打印出来。我觉得.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
在开头添加:

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)

我想我得到了你想要的。 我只是将所用字母的if语句移到while的顶部,而不是将guess变量附加到字母上,而是附加了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
print“欢迎来到刽子手!”
打印“
打印'--------------------------------------'
打印“
导入random#用于random.choice
dictionary=['CODING'、'GENEPEEKS'、'PYTHON'、'FREE TOM BRADY'、'BOSTON']
单词=随机选择(字典)
原始=列表(word)
临时=列表(word)
猜测=[]#空列表
LettersGuesed=[]
试验=int(0)#用于跟踪猜测
用户输入=“”
计数器=int(0)#跟踪列表中元素的位置(如果找到)
对于范围内的i(len(原始)):#创建'**…'列表
如果(原始[i]==''):
猜测。为元音添加(“”)#(空格)
其他:
猜猜。为所有其他字母加上(“”)#uu
打印猜测
而trial1:#检查多个字符
打印“错误:仅输入单个字符”
试用期-=1
持续
如果用户输入为原件:
当用户在temp中输入时:#循环检查冗余字符
计数器=临时索引(用户输入)
猜测[计数器]=用户输入
临时删除(用户输入)
温度插入件(计数器,“”)
计数器=0
对于范围(0,len(temp))内的i:#检查最终猜测是否与原始匹配
如果温度[i]=='':
计数器+=1
如果计数器==len(原始):#如果猜测与原始匹配
打印'Correct\t',猜测
打印“你赢了!”
试用=10
打破
打印'Correct\t',猜测,'\t左栏:',(9-trial)
其他:
试用期+=1
打印“不正确”,“\t左栏:”,(9-试用版)
其他:
打印“你输了!”
打印“正确答案为\t”,原件

您需要有一个特定的问题。什么不起作用?另外,您想要的和当前得到的示例输出也很有用。ok。我不熟悉编码和堆栈溢出,所以我不知道如何格式化以及响应者想要什么。谢谢你的建议!非常感谢!这真的很有帮助!还有一个问题。我如何使用.join()函数,以便它不打印[“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”。var=”“.join(“”*8),因此变量var为uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。它在每个空格后写8个下划线。