0

您好,我刚从高中开始编码,需要字符串方面的帮助。我想在文本文件中写入,其中创建的每个文本文件都会以 0.1 的增量增加,例如重量从 0.1 到 1(磅),并且长度从 0 到 80 增加 20 倍(英寸)。但是长度只会增加 20,直到之前的长度达到 1 磅。

为了进一步解释,我在下面编写的代码将创建一个 txt 文件。但它不会执行我想要它做的事情。我希望它能够执行我在下一句中要说的内容。在第一个 txt 文件中,文件为 weight_0.1_length_0_.txt,文件中为

  "weight of box 0.1"
  "length of box 0"

下一个 txt 文件是 weight_0.2_length_0_.txt,里面是

  "weight of box 0.2"
  "length of box 0"

依此类推,但是一旦权重达到 1,我希望长度增加到 20。所以值 20 的开始 txt 文件将 weight_0.1_length_20_.txt 并且在 txt 文件中将是

  "weight of box 0.1"
  "length of box 20"

并且将遵循此过程,直到您达到 80 的长度。我将拥有总共 50 个 txt 文件。

下面是我编写的代码,但仅将盒子的重量增加了 0.1。如果有人可以提供帮助,我将不胜感激。

  Weight = 0.1
  length = 0

  for i in range (10):
      input = 'weight_' + str(Weight) + '_length_' +str(length)+ '_'
      file = open(input + '.txt','w')

      file.write('weight of box ' +str(weight)+ '\n')
      file.write('length of box ' +str(length)+ '\n')
      file.close()

      Weight +=0.1
4

5 回答 5

1

您可以通过使用is_integer小数方法来做到这一点,该方法检查小数是否为整数,然后每当返回 true 时,将 20 添加到length.

这是为此的工作代码:

weight = 0.1
length = 0

while True:
    if length == 80: # check if length is 80, if so, break out of while loop
        break
    if (weight).is_integer():
        length += 20
    input = 'weight_' + str(weight) + '_length_' +str(length)+ '_'
    with open(input + '.txt','w') as file:

        file.write('weight of box ' +str(weight)+ '\n')
        file.write('length of box ' +str(length)+ '\n')

    weight = round(weight + 0.1, 1)

请注意,我使用round, 以确保不会出现舍入错误。

另外,我建议使用with open(.. ) as file而不是file=open(...). 后者可能会导致无法预料的问题,特别是如果您忘记调用file.close().

最后,我看不到以这种方式创建文件的实用程序。如果您只是将其作为一种学习体验,那么您最好使用print语句来检查您的价值观:

weight = 0.1
length = 0

while True:
    if length == 80:
        break
    if (weight).is_integer():
        length += 20
    input = 'weight_' + str(weight) + '_length_' +str(length)+ '_'
    weight = round(weight + 0.1, 1)

    print(input)
    print(weight)
    print(length)
于 2019-09-26T06:40:06.297 回答
1

你可以使用这个:

import numpy
Weight = float(0.1)
length = 0

while(length<=80):

    for i in numpy.arange(0.1,1.1,0.1):
        input = 'weight_' + str(i) + '_length_' +str(length)+ '_'
        file = open(input + '.txt','w')
        file.write('weight of box ' +str(Weight)+ '\n')
        file.write('length of box ' +str(length)+ '\n')
        file.close()
    length+=20
于 2019-09-26T06:53:59.177 回答
0

使用生成器:

# template for names to be generated
name_template = 'weight_%0.1f_length_%i_.txt'

# generator function tweak with your requirements.
def generate_names(total, max_length=80, initial_weight = 0.1, initial_length = 0, length_incrementor = 20):
  generated = 0
  weights = [round(x * 0.1, 1) for x in range(1, 11)]
  length = initial_length

  while generated < total:
    if length > max_length:
       return
    for weight in weights:
      yield name_template%(weight,length)
      generated += 1

    length += length_incrementor

total = 50

for name in generate_names(total):
  print(name)

输出:

weight_0.1_length_0_.txt
weight_0.2_length_0_.txt
weight_0.3_length_0_.txt
weight_0.4_length_0_.txt
..........
..........
weight_0.2_length_40_.txt
weight_0.3_length_40_.txt

https://realpython.com/introduction-to-python-generators/

于 2019-09-26T06:56:21.073 回答
0

您缺少长度的外环。

length = 0

for j in range (5):
    Weight = 0.1
    for i in range (10):
        input = 'weight_%.1f_length_%d_' % (Weight ,length)
        file = open(input + '.txt','w')

        file.write('weight of box %.1f\n' % Weight)
        file.write('length of box %.1f\n' % length)
        file.close()

        Weight +=0.1
    length += 20
于 2019-09-26T07:07:56.050 回答
0

试试下面的代码:

length = 0

for i in range(5):
    weight = 0
    for j in range(10):
        length = i * 20
        weight += 0.1
        input = 'weight_' + str(round(weight, 1)) + '_length_' +str(length)+ '_'

        with open(input + '.txt','w') as file:
            file.write('weight of box ' +str(round(weight, 1))+ '\n')
            file.write('length of box ' +str(length)+ '\n')
于 2019-09-26T07:27:46.440 回答