【问题标题】:Accessing variables inside a while loop在while循环中访问变量
【发布时间】:2015-11-18 11:01:19
【问题描述】:

我在使用循环计算粒子之间的距离时卡住了。到目前为止,我有下面的代码。除了最后输入的点之外,我似乎无法访问任何点。最后的打印只打印最后一个。

除了最后一个输入的点之外,还有什么方法可以访问输入的位置吗?

#user location
xu = int(input('X coordinate of user: '))
yu = int(input('Y coordinate of user: '))
zu = int(input('Z coordinate of user: '))
#number of particles
count = int(input())
icount = 0
while icount < count:
    x=  int(input('particle x coordinate: '))
    y = int(input('particle y coordinate: '))
    z = int(input('particle z coordinate: '))
    q = int(input('particle charge: '))
    icount += 1
print( x, y, z)        

【问题讨论】:

  • 既然定义了xu,yu,zu,为什么不用这些变量了呢?这些应该具有您的第一个值。
  • 我认为您想要一个列表并将值附加到该列表
  • 您需要为行 print(x,y,z) 缩进一个。也就是说,在最后一次打印之前插入一个制表符空格。一旦您能够获取值,请根据需要进行存储。
  • 澄清一下:每次你创建一个新的x= input .. 时,x 的旧值都会被覆盖,因为它只能存储一个值。
  • 在每个循环中 x,y,z 的值将被新的输入覆盖,因此最后一个循环值将在结束时保持

标签: python python-3.x


【解决方案1】:

实际上,它只会打印最后一点。

您正在遍历整个集合,一旦您完成了迭代,存储在您的xyz 中的唯一值就是您访问的最后一个元素中包含的值。

这是为什么呢?

因为每次存储新值时,都会覆盖旧值。

如果您在循环中包含您的 print 调用,您将显示所有值:

while icount < count:
    x = int(input('particle x coordinate: '))
    y = int(input('particle y coordinate: '))
    z = int(input('particle z coordinate: '))
    q = int(input('particle charge: '))
    icount += 1
    print( x, y, z)

【讨论】:

    【解决方案2】:

    您的变量 x、y 和 z 只是单个整数。如果您想使用索引存储多个值以区分它们,则需要使用列表。

    当您在循环中执行一系列类似操作时,通常需要列表。以下是如何执行此操作的示例,否则请尽可能接近您的代码:

    # user location
    xu = int(input('X coordinate of user: '))
    yu = int(input('Y coordinate of user: '))
    zu = int(input('Z coordinate of user: '))
    # number of particles
    count = int(input())
    X = []
    Y = []
    Z = []
    Q = []
    icount = 0
    while icount < count:
        X.append(int(input('particle x coordinate: ')))
        Y.append(int(input('particle y coordinate: ')))
        Z.append(int(input('particle z coordinate: ')))
        Q.append(int(input('particle charge: ')))
        icount += 1
    for x, y, z, q in zip(X, Y, Z, Q):
        print(x, y, z, q)
    

    本质上,我们正在制作一些列表来保存用户输入的所有粒子的值,然后使用 for 循环打印每个粒子,而不仅仅是保存的最后一个值。

    问题中的代码只是一遍又一遍地为变量分配不同的值,每次都丢弃前一个值。

    【讨论】:

    • @AdamSmith 我现在已经测试了代码,它应该可以工作。我试图快速向 OP 演示这个概念,而无需找到 python 解释器。
    【解决方案3】:

    您是否在遍历while 循环时尝试保存每个xyzq?您需要将这些值附加到一个列表(可能作为字典?)来做到这一点。

    count = int(input())
    
    loop_list = []
    for _ in range(count):  # this is equivalent!
        x = int(input('particle x coordinate: '))
        y = int(input('particle y coordinate: '))
        z = int(input('particle z coordinate: '))
        q = int(input('particle charge: '))
        loop_list.append({'x': x, 'y': y, 'z': z, 'q': q})
        # toss the dictionary of values into the list
    

    然后您可以通过执行以下操作来访问它:

    loop_list[3]['y']
    # the "y" input on the 4th iteration of the loop
    # (4th? remember zero-indexing!)
    

    为什么会有这些奇怪的双括号?因为loop_list 是一个充满字典的列表。 loop_list[3] 指向第 4 个字典,然后 ['y'] 从该字典中的键 'y' 获取值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-17
      • 1970-01-01
      • 2013-12-17
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多