[英]Needing insight to understanding strings in python
您好,我剛從高中開始編碼,需要字符串方面的幫助。 我想在文本文件中寫入,其中創建的每個文本文件都會以 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
您可以使用小數的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)
你可以使用這個:
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
使用生成器:
# 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)
Output:
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
您缺少長度的外環。
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
試試下面的代碼:
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.