【问题标题】:Is there a way to print out the itirated contents of my while loop all at once? Instead of one at a time?有没有办法一次打印出我的while循环的迭代内容?而不是一次一个?
【发布时间】:2019-12-23 20:12:44
【问题描述】:

我正在使用 while 循环来创建策略。但是,我希望政策是 最后一次性打印出来。目前,我只能让它们一张一张打印出来。

我尝试实现一个列表来尝试在其中存储信息以便稍后调用它。但是,我相信我正在超越我目前的知识。我是 python 和一般编程的新手。我愿意接受任何建议或意见。

这是针对 Python3 的。

我目前拥有的是以下

NumberOfPolicies = int(input ('How many Policies will you create?: '))
counter = 0
while counter < NumberOfPolicies:
    PartA = input('Paste in Part A of Policy: ')
    PartB = input ("Paste in Part B of Policy: ")
    PartC = input("Paste in Part C of policy: ")    
    NumberOfPolicies -= 1
    print ('\n it is ' + PartA + ' that will be translated to '+ 
    PartB+':'+PartC+'\n') 

输出如下。我希望首先提出所有问题,并在最后一次将输出全部吐出。

How many Policies will you create?: 2
Paste in Part A of Policy: 20
Paste in Part B of Policy: 20
Paste in Part C of policy: 20

 it is 20 that will be translated to 20:20

Paste in Part A of Policy: 30
Paste in Part B of Policy: 30
Paste in Part C of policy: 30

 it is 30 that will be translated to 30:30

【问题讨论】:

  • 将输入存储在一个列表中,然后对其进行迭代
  • 这是一个完美的解决方案。非常感谢。
  • 如果您喜欢这个解决方案,请点击解决方案左侧的“v”,这样我的努力就会得到回报;-)
  • 完成!再次感谢!

标签: python-3.x while-loop user-input


【解决方案1】:

也许可以这样做:

number_of_policies = int(input('How many Policies will you create?: '))
policies = []

for i in range(number_of_policies):
    parts = {}    
    parts['a'] = input('Paste in Part A of Policy: ')
    parts['b'] = input('Paste in Part B of Policy: ')
    parts['c'] = input('Paste in Part C of policy: ')    
    policies.append(parts)

for policy in policies:
    print(f"\n it is {policy['a']} that will be translated to {policy['b']}:{policy['c']}\n")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    相关资源
    最近更新 更多