Python 模块取消
Python module cancelation
我知道您可以在 Python 中调用模块本身,但是您也可以在其内部取消它吗?此代码应该只显示一次找到该名称。它会继续打印,直到我中断程序并从 IDLE 中获取 KeyboardInterrupt
。关于我应该添加或更改的内容,我能得到一些帮助吗? P.S.: 我有 Python 3.4.2
from array import *
s = ["Turner", "Philips", "Stevenson", "Jones", "Gonzalez",
"Whitaker", "Bruner", "Webster", "Foster", "Anderson",
"Klein", "Connors", "Rivers", "Wilson", "Duncan"]
g = [94, 82, 87, 78, 65,
90, 85, 97, 70, 100,
57, 88, 73, 92, 84]
s.sort()
for i in range(15):
idx = int(i)
studentList = s[idx]
gradeList = g[idx]
print(studentList, gradeList)
print("")
print("Student five: ", s[4], g[4])
print("Student ten: ", s[9], g[9])
print("Student fifteen: ", s[14], g[14])
gradeAverage = (g[0] + g[1] + g[2] + g[3] + g[4] + g[5] + g[6] + g[7] + g[8]
+ g[9] + g[10] + g[11] + g[12] + g[13] + g[14]) / 15
print("")
print("The average of the list of the grades is: ", gradeAverage)
print("The median of the list of grades is: ", g[7])
print("")
def byRank():
studentFound = False
index = 0
searchValue = input("Student to be found: ")
while(studentFound == False) & (index <= len(s) - 1):
if s[index] == searchValue:
studentFound == True
print("Name", searchValue, "was found at index", index)
else:
index = index + 1
print("Name not found. Searching...")
print("Name is not in the array.")
byRank()
studentFound == True
不会改变 studentFound
的值。使用单个 =
进行赋值。或者只使用 break
来处理像这样的简单情况。
您通常不会使用 while
循环来迭代 Python 中的序列 - 相反,如果您不想使用内置的 index
方法:
for index, candidate in enumerate(s):
if candidate == searchValue:
print("Name", searchValue, "was found at index", index)
break
但一次一个问题。
实现 byRank()
功能的正确方法是使用专为此目的而制作的 list.index()
方法。试试更像:
def byRank():
searchValue = input("Student to be found: ")
try:
index = s.index(searchValue)
except ValueError:
print("Name is not in the array.")
else:
print("Name", searchValue, "was found at index", index)
您的代码中有几个错误。最大的错误是你的 while 循环测试。这是修复程序
def byRank():
studentFound = False
index = 0
searchValue = raw_input("Student to be found: ")
while not studentFound and index < len(s):
if s[index] == searchValue:
studentFound = True
print("Name ", searchValue, " was found at index ", index)
else:
index += 1
print("Name not found. Searching...")
print("Name is not in the array.")
仅供参考 -- 如果您愿意放弃您的工作,Tom Hunt 的回答是编写代码的更好方法。
我知道您可以在 Python 中调用模块本身,但是您也可以在其内部取消它吗?此代码应该只显示一次找到该名称。它会继续打印,直到我中断程序并从 IDLE 中获取 KeyboardInterrupt
。关于我应该添加或更改的内容,我能得到一些帮助吗? P.S.: 我有 Python 3.4.2
from array import *
s = ["Turner", "Philips", "Stevenson", "Jones", "Gonzalez",
"Whitaker", "Bruner", "Webster", "Foster", "Anderson",
"Klein", "Connors", "Rivers", "Wilson", "Duncan"]
g = [94, 82, 87, 78, 65,
90, 85, 97, 70, 100,
57, 88, 73, 92, 84]
s.sort()
for i in range(15):
idx = int(i)
studentList = s[idx]
gradeList = g[idx]
print(studentList, gradeList)
print("")
print("Student five: ", s[4], g[4])
print("Student ten: ", s[9], g[9])
print("Student fifteen: ", s[14], g[14])
gradeAverage = (g[0] + g[1] + g[2] + g[3] + g[4] + g[5] + g[6] + g[7] + g[8]
+ g[9] + g[10] + g[11] + g[12] + g[13] + g[14]) / 15
print("")
print("The average of the list of the grades is: ", gradeAverage)
print("The median of the list of grades is: ", g[7])
print("")
def byRank():
studentFound = False
index = 0
searchValue = input("Student to be found: ")
while(studentFound == False) & (index <= len(s) - 1):
if s[index] == searchValue:
studentFound == True
print("Name", searchValue, "was found at index", index)
else:
index = index + 1
print("Name not found. Searching...")
print("Name is not in the array.")
byRank()
studentFound == True
不会改变 studentFound
的值。使用单个 =
进行赋值。或者只使用 break
来处理像这样的简单情况。
您通常不会使用 while
循环来迭代 Python 中的序列 - 相反,如果您不想使用内置的 index
方法:
for index, candidate in enumerate(s):
if candidate == searchValue:
print("Name", searchValue, "was found at index", index)
break
但一次一个问题。
实现 byRank()
功能的正确方法是使用专为此目的而制作的 list.index()
方法。试试更像:
def byRank():
searchValue = input("Student to be found: ")
try:
index = s.index(searchValue)
except ValueError:
print("Name is not in the array.")
else:
print("Name", searchValue, "was found at index", index)
您的代码中有几个错误。最大的错误是你的 while 循环测试。这是修复程序
def byRank():
studentFound = False
index = 0
searchValue = raw_input("Student to be found: ")
while not studentFound and index < len(s):
if s[index] == searchValue:
studentFound = True
print("Name ", searchValue, " was found at index ", index)
else:
index += 1
print("Name not found. Searching...")
print("Name is not in the array.")
仅供参考 -- 如果您愿意放弃您的工作,Tom Hunt 的回答是编写代码的更好方法。