简体   繁体   English

如何将整数迭代加在一起

[英]How do you add integer iterations together

This function prints a pyramid made of "*" and "&" characters. 此功能打印由“ *”和“&”字符组成的金字塔。 I want the program to print out the total number of "&" characters in the pyramid, so I used .count to count how many times "&" appears in each row. 我希望程序打印出金字塔中“&”字符的总数,因此我使用.count来计数每行中“&”出现的次数。 The problem is that I can't sum together the output from variable z to get the total time "&" appears in the pyramid. 问题是我无法将变量z的输出求和,以使总时间“&”出现在金字塔中。 Any ideas? 有任何想法吗?

import random
from random import choice
from random import randint

def pyramid():
    min_pyramid_size = 5
    max_pyramid_size = 10
    num_rows = random.randint(min_pyramid_size, max_pyramid_size)
    for i in range(num_rows):
        x = ''.join(str(random.choice('*&')) for j in range(2*i+1))
        print(' ' * (num_rows - i) + x)
        z = x.count('&')
        print(z)

pyramid()

UPDATE: I tried moving z out of the loop and it just prints the number of "&" in the last row. 更新:我尝试将z移出循环,它仅在最后一行打印“&”号。

import random
from random import choice
from random import randint

def pyramid():
    min_pyramid_size = 3
    max_pyramid_size = 5
    num_rows = random.randint(min_pyramid_size, max_pyramid_size)
    for i in range(num_rows):
        x = ''.join(str(random.choice('*&')) for j in range(2*i+1))
        print(' ' * (num_rows - i) + x)
    z = 0
    z += x.count('&')
    print(z)

pyramid()

Haven't run it, but you need a variable outside the for loop that will hold the total number of '&'s. 尚未运行它,但是您需要在for循环之外的变量来保存'&'的总数。

def pyramid():
    min_pyramid_size = 5
    max_pyramid_size = 10
    num_rows = random.randint(min_pyramid_size, max_pyramid_size)
    total_z = 0
    for i in range(num_rows):
        x = ''.join(str(random.choice('*&')) for j in range(2*i+1))
        print(' ' * (num_rows - i) + x)
        z = x.count('&')
        total_z = total_z + z
        print(z)

pyramid()

define z outside the scope of the for loop 在for循环范围之外定义z

z = 0

and use 和使用

z += x.count('&')

rather than 而不是

z = x.count('&')

Try this:- 尝试这个:-

import random
from random import choice
from random import randint

def pyramid():
    min_pyramid_size = 5
    max_pyramid_size = 10
    z = 0
    num_rows = random.randint(min_pyramid_size, max_pyramid_size)
    for i in range(num_rows):
        x = ''.join(str(random.choice('*&')) for j in range(2*i+1))
        print(' ' * (num_rows - i) + x)
        z = z + x.count('&')
    print(z)

pyramid()

Here's a more compact solution using a list comprehension: 这是使用列表推导的更紧凑的解决方案:

def pyramid(min_size=5, max_size=10):
    N = randint(min_size, max_size)
    x = ''.join(' ' * (N-i) + ''.join(choice('*&') for j in range(2*i+1)) + '\n' for i in range(N))
    print(x)
    print(x.count('&'))

Demo: 演示:

>>>  pyramid()
         &
        &*&
       &**&*
      &&&**&*
     &***&&&&&
    &&&&*&&****
   &&&&*&***&&**
  *&&&&**&&&&&**&
 &*&***&*&&&*&&*&*

47

You could change the values of the minimum and maximum size of the pyramid by passing the appropriate arguments to the function, for example pyramid(min_size=8, max_size=12) . 您可以通过将适当的参数传递给函数来更改金字塔的最小和最大大小的值,例如pyramid(min_size=8, max_size=12)

You need to have two variables, one for the internal and one for the external count. 您需要有两个变量,一个用于内部变量,一个用于外部计数。

def pyramid():
  min_pyramid_size = 5
  max_pyramid_size = 10
  num_rows = random.randint(min_pyramid_size, max_pyramid_size)
  outer_z = 0
  for i in range(num_rows):
    x = ''.join(str(random.choice('*&')) for j in range(2*i+1))
    print(' ' * (num_rows - i) + x)
    inner_z = x.count('&')  # Alternatively calculate inner_z twice, but don't store it:
    print(inner_z)          # print(x.count('&'))
    outer_z += inner_z      # outer_z += x.count('&')
  print(outer_z)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM