Python 3.x 接受列表,拆分子列表中的元素并一起打印
目前正在通过freecodecamp.org和其他资源学习Python。我很难理解在子列表中拆分列表并对其进行迭代的概念。我有一份不同练习的作业,需要按顺序打印 问题是,当输入只有一个练习时,我让它工作。我只是无法通过给它一个列表来让它工作。我错过了什么Python 3.x 接受列表,拆分子列表中的元素并一起打印,python-3.x,Python 3.x,目前正在通过freecodecamp.org和其他资源学习Python。我很难理解在子列表中拆分列表并对其进行迭代的概念。我有一份不同练习的作业,需要按顺序打印 问题是,当输入只有一个练习时,我让它工作。我只是无法通过给它一个列表来让它工作。我错过了什么 def arithmetic_arranger(problems, answer=False): for som in problems: sumlist = list(som.split())
def arithmetic_arranger(problems, answer=False):
for som in problems:
sumlist = list(som.split())
getal_a=(sumlist[0])
operatie= sumlist[1]
getal_b=(sumlist[2])
#reformat = "{:>9}\n{:>3} {:>2}\n -----".format(getal_a, operatie, getal_b)
getal_1=int(getal_a)
getal_2=int(getal_b)
if answer == False:
antwoord=''
if answer==True:
if operatie.strip() == '+':
antwoord=getal_1 + getal_2
elif operatie.strip() == '-':
antwoord=getal_1-getal_2
elif operatie.strip() == '*' or operatie.strip() == 'x':
antwoord=getal_1*getal_2
elif operatie.strip() == ':':
antwoord=getal_1/getal_2
reformat = "{:>9}\n{:>3} {:>2}\n -----\n {:>8}".format(getal_1, operatie, getal_2, antwoord)
return reformat
用于调用函数:当我还需要答案时,我使用True;当必须隐藏答案时,我使用False
practise=["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]
print(arithmetic_arranger(practise, True))
我当前和首选的结果是这样的,但它似乎只得到列表中的第一个元素。所以,对我来说,这感觉就像我非常接近,但似乎无法修复它而不破坏我目前的结果
32
+ 698
-----
730
预期:
32 3801
+ 698 - 2
----- ----- etc
730 3799
试着和新同事一起工作。使您的线路更容易理解
reformat = "{:>9}\n{:>3} {:>2}\n -----\n {:>8}".format(getal_1, operatie, getal_2, antwoord)
将成为
reformat = f"{getal_1:>9}\n{operatie:>3} {getal_2:>2}\n -----\n {antwoord:>8}"
如您所见,您正在输出一个加法。您需要将其全部放在一个巨大的重新格式化字符串中,这会让人感到困惑。为什么不将其写入csv文件或解析为html
其他一些小提示:
if answer==True:
因为您的变量答案已经是布尔值了,所以只需编写
如果回答:
或如果没有回答:
,则相反
无需将.split()
放入列表()
中,因为split已返回列表。结果也是条带化的。也不需要.strip()
重新考虑你的代码。包括输入
def test(a):
results = []
add = lambda x,y: int(x) + int(y)
sub = lambda x,y: int(x) - int(y)
mul = lambda x,y: int(x) * int(y)
div = lambda x,y: int(x) / int(y)
for i in a:
x = i.split(" ")
if x[1] == '+':
v = add(x[0],x[2])
elif x[1] == '-':
v = sub(x[0],x[2])
elif x[1] == '*':
v = mul(x[0],x[2])
elif x[1] == '/':
v = div(x[0],x[2])
# formating output string
dashes = ""
if len(x[2]) > len(x[0]):
x[2], x[0] = (x[0], x[2])
dif = len(x[0]) - len(x[2])
for _ in range(dif):
x[2] = " " + x[2]
for _ in range(len(x[0])):
dashes += "-"
result = f" {x[0]}\n{x[1]} {x[2]}\n {dashes}\n {v}"
results.append(result)
return results
a = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]
answers = test(a)
for answer in answers:
print(f"{answer}\n")
输出
698
+ 32
---
730
3801
- 2
----
3799
45
+ 43
--
88
123
+ 49
---
172
函数接受输入并返回输出,当它们返回输出时,它们的工作完成,它们退出,解释器继续执行程序的其余部分 函数返回列表第一个成员的重新格式,并退出,因为return语句位于循环内
您可以考虑通过循环打印ReFrand的每一次迭代,或者如果您想存储它,则将ReFrad附加到一个列表中,在完成列表之后,将其返回到循环之外。
然后,您可以在返回的列表中循环打印每个成员def arithmetic_arranger(problems, answer=False):
reformat_list = [] #/////////////////////////////////////////////////
for som in problems:
sumlist = list(som.split())
getal_a=(sumlist[0])
operatie= sumlist[1]
getal_b=(sumlist[2])
#reformat = "{:>9}\n{:>3} {:>2}\n -----".format(getal_a, operatie, getal_b)
getal_1=int(getal_a)
getal_2=int(getal_b)
if answer == False:
antwoord=''
if answer==True:
if operatie.strip() == '+':
antwoord=getal_1 + getal_2
elif operatie.strip() == '-':
antwoord=getal_1-getal_2
elif operatie.strip() == '*' or operatie.strip() == 'x':
antwoord=getal_1*getal_2
elif operatie.strip() == ':':
antwoord=getal_1/getal_2
reformat = "{:>9}\n{:>3} {:>2}\n -----\n {:>8}".format(getal_1, operatie, getal_2, antwoord)
reformat_list.append(reformat) #///////////////////////////////
return reformat_list #///////////////////////////////// changed indentation
然后调用函数:
practise=["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]
list_of_reformated_arithmetic= arithmetic_arranger(practise, True)
for i in range(len(list_of_reformated_arithmetic)):
print(something[i], end = "\n\n")
其结果将是:
32
+ 698
-----
730
3801
- 2
-----
3799
45
+ 43
-----
88
123
+ 49
-----
172
并排打印更具挑战性,我不知道如何操作。此解决方案几乎完美无瑕!问题是赋值声明它返回安排好的问题,这意味着您对函数的调用必须集成在其中。这可能吗?我已经使用了您的反馈和技巧,并结合下面的注释帮助我改进了代码。虽然如此接近!非常感谢您的反馈。我同意,它使代码更加清晰。我已将您的提示集成到我的当前代码中。通过在返回之前使用打印(报表),我已经达到了预期的结果。是否可以让函数返回for循环打印语句?