python用户输入q退出,python
我有以下代码根据用户输入从给定的文本文件中提取数据:def read_file():
counties = []
f = open('file.txt', 'r')
for line in f.readlines():
data = {}
lines = ' '.join(line.split()).split()
data[''] = int(lines[5])
data[''] = float(lines[3])
data[''] = float(lines[2])
data[''] = line[1:6].rstrip(' ')
file.append(data)def info_search(file):
user_search = input('Search for a word: ').lower()
while user_search != 'q' and user_search != 'quit':
for f in file:
if user_search in file['Data Info'].lower():
print(data_info)
break
user_search = input('Search for a word: ').lower()
此程序返回以下输出:>>> Search for a word:
>>> allen
>>> word: Allen, Age: 12, Height: 5'4, GPA: 3.8
>>> Search for a word:
>>> quit
>>>
输入将提示程序根据用户输入的单词从文本文件中提取特定数据。这一切都很好。我无法弄清楚如何添加一个功能,如果在文件中找不到该单词,则会提示您再次输入。
现在,如果在文件中未找到该单词,该程序将要求再次搜索。但是,在要求其他搜索之前,我需要说一些类似“在文本文件中找不到单词”之类的内容。
这是我正在寻找的输出:>>> Search for a word:
>>> Jim
>>> Word: Jim, Age: 20, Height: 6'1, GPA: 2.5
>>> Search for a word:
>>> Molly
>>> Your searched word could not be found.
>>> Search for a word:
>>> Eddy
>>> Word: Eddy, Age: 15, Height: 5'6, GPA: 4.0
>>> Search for a word:
>>> quit
>>>
我觉得这样的事情应该起作用:def info_search(file):
user_search = input('Search for a word: ').lower()
while user_search != 'q' and user_search != 'quit':
for f in file:
if user_search in file['Data Info'].lower():
print(data_info)
user_search = input('Search for a word: ').lower()
else:
print("Your searched word could not be found.")
user_search = input('Search for a word: ').lower()
但是它只是输出无论我输入什么词都找不到您搜索到的词。即使输入在第一个代码中起作用的单词,现在也不再起作用。即使键入quit也不再退出循环。请帮忙。
新输出:>>> Search for a word:
>>> Allen
>>> Your searched word could not be found.
>>> Search for a word:
>>> quit
>>> Your searched word could not be found.
>>> Search for a word:
python用户输入q退出,python
我有以下代码根据用户输入从给定的文本文件中提取数据:def read_file():
counties = []
f = open('file.txt', 'r')
for line in f.readlines():
data = {}
lines = ' '.join(line.split()).split()
data[''] = int(lines[5])
data[''] = float(lines[3])
data[''] = float(lines[2])
data[''] = line[1:6].rstrip(' ')
file.append(data)def info_search(file):
user_search = input('Search for a word: ').lower()
while user_search != 'q' and user_search != 'quit':
for f in file:
if user_search in file['Data Info'].lower():
print(data_info)
break
user_search = input('Search for a word: ').lower()
此程序返回以下输出:>>> Search for a word:
>>> allen
>>> word: Allen, Age: 12, Height: 5'4, GPA: 3.8
>>> Search for a word:
>>> quit
>>>
输入将提示程序根据用户输入的单词从文本文件中提取特定数据。这一切都很好。我无法弄清楚如何添加一个功能,如果在文件中找不到该单词,则会提示您再次输入。
现在,如果在文件中未找到该单词,该程序将要求再次搜索。但是,在要求其他搜索之前,我需要说一些类似“在文本文件中找不到单词”之类的内容。
这是我正在寻找的输出:>>> Search for a word:
>>> Jim
>>> Word: Jim, Age: 20, Height: 6'1, GPA: 2.5
>>> Search for a word:
>>> Molly
>>> Your searched word could not be found.
>>> Search for a word:
>>> Eddy
>>> Word: Eddy, Age: 15, Height: 5'6, GPA: 4.0
>>> Search for a word:
>>> quit
>>>
我觉得这样的事情应该起作用:def info_search(file):
user_search = input('Search for a word: ').lower()
while user_search != 'q' and user_search != 'quit':
for f in file:
if user_search in file['Data Info'].lower():
print(data_info)
user_search = input('Search for a word: ').lower()
else:
print("Your searched word could not be found.")
user_search = input('Search for a word: ').lower()
但是它只是输出无论我输入什么词都找不到您搜索到的词。即使输入在第一个代码中起作用的单词,现在也不再起作用。即使键入quit也不再退出循环。请帮忙。
新输出:>>> Search for a word:
>>> Allen
>>> Your searched word could not be found.
>>> Search for a word:
>>> quit
>>> Your searched word could not be found.
>>> Search for a word: