2017-09-05 3 views
0

「100セントワード」の問題を解くプログラムを作成しようとしています https://www.experts-exchange.com/questions/20372967/A-1-B-2-C-3-etc-Find-words-that-equal-100.html私はユーザーに単語の価値を決定させたいのですが、 です。 ここまでは私のコードです。

__author__ = "Anthony Chen" 
__copyright__ = "Copyright (C) 2017 Anthony Chen" 
__license__ = "Public Domain" 
__version__ = "0.5" 

linenumber = 1 
global filename 
filename = "wordlist.txt" 
global infile 
infile = open(filename, "r") 
global filelinelist 
filelinelist = [] 
global fileline 
fileline = infile.readline(int(linenumber)) 
global fileworth 
lineworth = input('How much cents do you want your words to be worth?') 
global wordlist 
wordlist = [] 

def process(): 
    fileline = filename.lower() 
    fileline.rstrip('/n') 
    fileline.rstrip("'") 
    filelinelist = list(fileline) 
    filelinelist = fileline.replace('a', '1') 
    filelinelist = fileline.replace('b', '2') 
    filelinelist = fileline.replace('c', '3') 
    filelinelist = fileline.replace('d', '4') 
    filelinelist = fileline.replace('e', '5') 
    filelinelist = fileline.replace('f', '6') 
    filelinelist = fileline.replace('g', '7') 
    filelinelist = fileline.replace('h', '8') 
    filelinelist = fileline.replace('i', '9') 
    filelinelist = fileline.replace('j', '10') 
    filelinelist = fileline.replace('k', '11') 
    filelinelist = fileline.replace('l', '12') 
    filelinelist = fileline.replace('m', '13') 
    filelinelist = fileline.replace('n', '14') 
    filelinelist = fileline.replace('o', '15') 
    filelinelist = fileline.replace('p', '16') 
    filelinelist = fileline.replace('q', '17') 
    filelinelist = fileline.replace('r', '18') 
    filelinelist = fileline.replace('s', '19') 
    filelinelist = fileline.replace('t', '20') 
    filelinelist = fileline.replace('u', '21') 
    filelinelist = fileline.replace('v', '22') 
    filelinelist = fileline.replace('w', '23') 
    filelinelist = fileline.replace('x', '24') 
    filelinelist = fileline.replace('y', '25') 
    filelinelist = fileline.replace('z', '26') 

print('This is a program made to solve the __cents problem.') 
print(' ') 
print('If a is worth 1 cents, b is worth 2 cents, c is worth 3 cents, then list all thewords with a total value of __ cents.') 

while True: 
    infile = open(filename, "r") 
    fileline = infile.readline(int(linenumber)) 
    linenumber += 1 
    process() 
    filesum = sum(filelinelist) 
    if int(filesum) == lineworth: 
     wordlist.append(filelist.join) 
     print('The words found so far are: %s" % wordlist') 
     print(wordlist.count) 
     print('words found.') 
    else: 
     filelinelist = [] 

私はそれを実行した場合、これは、最大を示しています。 あなたの言葉はこれが__centsの問題を解決するためになされたプログラムである12 の価値があるとどのくらいセントたいですか?。

(1セントの価値がある場合は、bが2セントの価値がある、cは3セントの価値がある、その後、__セントの合計値ですべての単語を一覧表示します。)

Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit 
(AMD64)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> 
=========== RESTART: C:\Anthony\School\__CentsWord\__CentsWord.py 
=========== 
How much cents do you want your words to be worth?100 
This is a program made to solve the __cents problem. 

If a is worth 1 cents, b is worth 2 cents, c is worth 3 cents, then list all 
thewords with a total value of __ cents. 

他に何も起こりません。できるだけ早く私に戻ってください。ありがとう。

答えて

0

すぐに問題が起こります。replaceは、リストを取得する方法ではありません。タイプstrには置換機能がありますが、明示的に文字列をリストに変換しました。

お試し ファイルリストお手数ですがお試しください。

各文字を数値ではなくstrに置き換えたので、合計にはいくつか問題があります。 sumには文字列のリストはありません。 sumにする前にそれぞれを整数に変換する必要があります。

辞書について知っている場合は、{ 'a':1, 'b':2, ...}のような翻訳辞書を考えてください。また、文字値へのエンコーディングを簡素化するために、ordchr関数を使用することを検討してください。

あなたのコードで非常に多くのことが起こっている場合には、作業中のプログラムに一歩踏み出そうとするのではなく、この提案を残しておきます。

+0

他に知りたいことがありますか? –

関連する問題