Python 查找';Y';或';N';一串
如何使两个字符串正确比较?因此,如果字符串Python 查找';Y';或';N';一串,python,string,printing,comparison,Python,String,Printing,Comparison,如何使两个字符串正确比较?因此,如果字符串Answers中包含'N',则会有一个输出 def stringSearcher(): #Seperates and returns 2 strings from .txt file f = open("QuestionTree.txt", "r") currentLine = f.readline() for line in f: if Answer in line:
Answers
中包含'N'
,则会有一个输出
def stringSearcher(): #Seperates and returns 2 strings from .txt file
f = open("QuestionTree.txt", "r")
currentLine = f.readline()
for line in f:
if Answer in line:
yesnos, answerOrQuestion = line.split(',')
return answerOrQuestion, yesnos
while True:
Answer = raw_input("Y or N: ") #User input
answerOrQuestion, yesnos = stringSearcher() #allows usage of Return variables
if yesnos == Answer: #Will compare the 'Y' in string but not
print answerOrQuestion
这是输出的样子:
Y或N:N#不打印文本文件中的N
Y还是N:Y
在塔楼里?
Y或N:YN
Y或N:YY
老塔?
Y或N:YYY
7层楼高?
是或否:^CTraceback(最近一次呼叫最后一次):
文件“TestCode.py”,第11行,在
回答=原始输入(“Y或N:”)
这是文本文件的外观:
是宿舍吗? Y、 在塔楼里? YY,老塔? YYY,7层楼高? YYYY,是凯莉吗? YYYYY,凯里大厅 YYYYN,特劳特曼厅 YYYN,是惠勒吗? N、 使命之西? 纽约,布鲁姆菲尔德的人? 纽约,CMU所有? NYYY,Kewadin 尼恩,B球场? 尼尼,是列克星敦吗?变化
def stringSearcher():
到
你的问题是什么?当我运行“执行我的程序”时,它只会给出“Y”的输出。是的,但你的问题是什么?你想知道为什么吗?你想知道如何改变这种行为吗?问我们一个问题。你应该在你的问题中加上这个,这样人们就会知道如何回答你。它不起作用。。。TypeError:string Searcher()只接受1个参数(给定0),同时在while循环中将stringSearcher()更改为stringSearcher(Answer)。这些更改没有更改包含“N”的字符串的输出,我该如何修复它?您能给我看一下.txt文件中包含“N”的示例字符串行吗?我无法修复它的格式,每次我尝试时,我都会得到一个“你有很多代码,为什么不添加一些细节?”
def stringSearcher():
def stringSearcher(Answer):
import re
def stringSearcher(Answer):
f = open("QuestionTree.txt", "r")
currentLine = f.readline()
for line in f:
if re.match(Answer, line):
yesnos, answerOrQuestion = line.split(',')
return answerOrQuestion, yesnos
while True:
Answer = raw_input("Y or N: ")
answerOrQuestion, yesnos = stringSearcher(Answer)
if Answer == yesnos: #Will compare the 'Y' in string but not
print answerOrQuestion