我需要帮助的领域是 第2步:从此链接获取项目 http://wiki.openhatch.org/Scrabble_challenge#Step_2:_get_the_rack 到目前为止我的代码是:
import argparse
import sys
file=open('sowpods.txt','r')
scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
parser=argparse.ArgumentParser(description='designed to help in scrabble')
#command line interface using argparse module
parser.add_argument("word",help="input your letters")#adds the argument with
description
args=parser.parse_args()
print(args.word)
如果有人没有输入这封信,我无法弄清楚是谁打印错误信息,我试着
if args.word=='':
print('error need letters')
sys.exit()
但这不起作用。谢谢你的帮助。
答案 0 :(得分:0)
首先,使答案可选,以避免argparse抛出错误。如果没有传递参数,它将被设置为None
(或其他默认值)。
parser=argparse.ArgumentParser(description='designed to help in scrabble')
parser.add_argument("word",help="input your letters", nargs='?') # nargs
args=parser.parse_args()
if args.word is None: # check equality to None
print('error need letters')
sys.exit()