Python - 错误检查
Python - Error Checking
假设我有一个代码片段:
players_chosen_hit = int(input('Where do you want to try to hit the AI?: 1-9 '))
如果用户输入字母怎么办?我该如何处理才能告诉用户他搞砸了并让他重新输入直到他输入号码?
这个怎么样:
possibleusershipplaces = [1,2,3,4,5,6,7,8,9]
players_chosen_hit = int(input('Where do you want to try to hit the AI?: 1-9 '))
while players_chosen_hit not in possibleusershipplaces:
players_chosen_hit = input('Please tell me where the hit is: 1-9 (or Ctrl/Command+C to quit) ')
players_chosen_hit = int(players_chosen_hit)
possibleusershipplaces = {1,2,3,4,5,6,7,8,9}
while True:
try:
players_chosen_hit = int(input('Where do you want to try to hit the AI?: 1-9 '))
if players_chosen_hit in possibleusershipplaces:
break
except ValueError:
print("Invalid entry")
同时处理退出:
while True:
try:
players_chosen_hit = input('Please tell me where the hit is: 1-9 (q to quit) ')
if players_chosen_hit == "q":
print("Goodbye")
break
players_chosen_hit = int( players_chosen_hit)
if players_chosen_hit in possibleusershipplaces:
break
except ValueError:
print("Invalid entry")
如果您不想要 try/except 并且只有正数,您可以使用 str.isdigit
但 try/except 是惯用的方式:
possibleusershipplaces = {"1","2","3","4","5","6","7","8","9"}
for players_chosen_hit in iter(lambda:input('Please tell me where the hit is: 1-9 (q to quit) '),"q"):
if players_chosen_hit.isdigit() and players_chosen_hit in possibleusershipplaces:
players_chosen_hit = int(players_chosen_hit)
iter
接受第二个参数 sentinel
如果输入,它将打破循环。
当我们达到条件时,使用函数和 return 可能会更好:
def get_hit():
while True:
try:
players_chosen_hit = input('Please tell me where the hit is: 1-9 (q to quit) ')
if players_chosen_hit == "q":
print("Goodbye")
return
players_chosen_hit = int(players_chosen_hit)
if players_chosen_hit in possibleusershipplaces:
return players_chosen_hit
except ValueError:
print("Invalid entry")
假设我有一个代码片段:
players_chosen_hit = int(input('Where do you want to try to hit the AI?: 1-9 '))
如果用户输入字母怎么办?我该如何处理才能告诉用户他搞砸了并让他重新输入直到他输入号码?
这个怎么样:
possibleusershipplaces = [1,2,3,4,5,6,7,8,9]
players_chosen_hit = int(input('Where do you want to try to hit the AI?: 1-9 '))
while players_chosen_hit not in possibleusershipplaces:
players_chosen_hit = input('Please tell me where the hit is: 1-9 (or Ctrl/Command+C to quit) ')
players_chosen_hit = int(players_chosen_hit)
possibleusershipplaces = {1,2,3,4,5,6,7,8,9}
while True:
try:
players_chosen_hit = int(input('Where do you want to try to hit the AI?: 1-9 '))
if players_chosen_hit in possibleusershipplaces:
break
except ValueError:
print("Invalid entry")
同时处理退出:
while True:
try:
players_chosen_hit = input('Please tell me where the hit is: 1-9 (q to quit) ')
if players_chosen_hit == "q":
print("Goodbye")
break
players_chosen_hit = int( players_chosen_hit)
if players_chosen_hit in possibleusershipplaces:
break
except ValueError:
print("Invalid entry")
如果您不想要 try/except 并且只有正数,您可以使用 str.isdigit
但 try/except 是惯用的方式:
possibleusershipplaces = {"1","2","3","4","5","6","7","8","9"}
for players_chosen_hit in iter(lambda:input('Please tell me where the hit is: 1-9 (q to quit) '),"q"):
if players_chosen_hit.isdigit() and players_chosen_hit in possibleusershipplaces:
players_chosen_hit = int(players_chosen_hit)
iter
接受第二个参数 sentinel
如果输入,它将打破循环。
当我们达到条件时,使用函数和 return 可能会更好:
def get_hit():
while True:
try:
players_chosen_hit = input('Please tell me where the hit is: 1-9 (q to quit) ')
if players_chosen_hit == "q":
print("Goodbye")
return
players_chosen_hit = int(players_chosen_hit)
if players_chosen_hit in possibleusershipplaces:
return players_chosen_hit
except ValueError:
print("Invalid entry")