Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python +;的操作数类型不受支持:while循环中的int和str_Python_Python 3.x - Fatal编程技术网

Python +;的操作数类型不受支持:while循环中的int和str

Python +;的操作数类型不受支持:while循环中的int和str,python,python-3.x,Python,Python 3.x,嘿,伙计们,我们正在尝试创建一个函数,该函数包含两个参数,n和high,并将n^1 n^2的值打印到n^high(在一行上)-任何帮助都将非常感谢 def print_powers(n, high): i = 0 new = '' count = 0 while i < high: count += 1 while count <= high: new += int((n)**count) + '

嘿,伙计们,我们正在尝试创建一个函数,该函数包含两个参数,n和high,并将n^1 n^2的值打印到n^high(在一行上)-任何帮助都将非常感谢

def print_powers(n, high):
    i = 0
    new = ''
    count = 0
    while i < high:
        count += 1
        while count <= high:
           new += int((n)**count) + ' '
        i += 1
    return new

print(print_powers(2, 5))
def打印功率(n,高):
i=0
新=“”
计数=0
而我<高:
计数+=1

而count
n**count
已经是一个
int
。在将其添加到
''
之前,需要将其转换为
str
。这应该是
new+=str(n**count)+'

在追加空格之前,需要将整数转换为字符串

new += str(int((n)**count)) + ' '

对于你的具体问题,你有一些答案。另外,与其编写一个函数来计算和打印功率,我建议您编写一个函数来计算功率并返回一个值列表:例如,
[2,4,8,16,32]
。然后,您可以对该数据列表执行任何操作,包括将其发送到另一个函数以将它们连接到以空格分隔的字符串和/或打印。换句话说,计算和打印是非常不同的,所以要将它们分开。