私はいくつかの関数を定義した文書を持っています(assignment_2a.py)。私は、次のことを達成しようとしている:コマンドラインからPythonプログラムを実行し、引数を指定する
Modify the [assignment_2a.py](../code/assignment_2a.py) to be able to run
the program `make_random_story` from the command line:
```
$ python assignment_2a.py '../data/alice.txt' 2 200
```
私はipythonに行くことによって、この指定された関数を実行する方法を知っている、それが正常に動作します。しかし、上記のコマンドを使用してコマンドラインから実行できるように、つまりその形式で入力変数を指定するために、ドキュメントを変更するにはどうすればよいですか?
UPDATE:私はコメントで示唆したように機能を更新し、次のエラーメッセージを取得しています:
COMMAND LINE INPUT:
python assignment_2a.py '../data/alice.txt' 2 200
COMMAND LINE OUTPUT:
Traceback (most recent call last):
File "assignment_2a.py", line 202, in <module>
make_random_story(*sys.argv[1:])
File "assignment_2a.py", line 189, in make_random_story
for i in xrange(0,num_words):
TypeError: an integer is required
そしてここでは、関連する更新機能のテキストです:
def make_random_story(f, n_gram=2, num_words=200):
f = open(f)
random.seed('Is the looking-glass is half full or half-empty?')
story = ''
if n_gram==1:
d = associated_unigrams(f)
elif n_gram==2:
d = associated_bigrams(f)
elif n_gram==3:
d = associated_trigrams(f)
for i in xrange(0,num_words):
chosenkey = random.choice(d.keys())
story += random.choice(d[chosenkey]) + " "
print story
f.close()
if __name__ == '__main__':
make_random_story(*sys.argv[1:])
[mcve]を作成してください。 'sys.argv'をリストとして出力してみてください...あなたは何を気付きますか? –
この質問の回答を受け入れることを検討してください。https://stackoverflow.com/questions/44729412/creating-a-dictionary-for-each-word-in-a-file-and-counting-the-frequency-of-word –