如果a = 0.01 $,b = 0.02 $等,如何使程序存档每个单词的值

时间:2017-09-05 23:49:42

标签: python debugging

我正在尝试制定一个程序,以找出“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 = []

如果我运行它,会显示: 你希望你的话值多少钱?12 这是一个解决__cents问题的程序。

(如果a值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.

没有其他事情发生。请尽快回复我。感谢。

1 个答案:

答案 0 :(得分:0)

直接问题正是它告诉你的:replace不是你用列表获得的方法。类型str具有替换功能,但您已将字符串显式转换为列表。

尝试print filelinelist ,向您展示您正在使用的内容。

你也会遇到一些麻烦,因为你用str替换了每个字母,而不是数字值。你不能sum一个字符串列表;您必须先将每个转换为整数,然后才能sum它们。

如果您了解字典,可以考虑翻译字典,例如{ 'a':1, 'b':2, ...}。另请考虑使用ordchr函数将编码简化为字母值。

我会在此处留下建议,而不是在你的代码中有很多东西要重新工作时,不要试图一直提升你的工作程序。