Python +;的操作数类型不受支持:while循环中的int和str
嘿,伙计们,我们正在尝试创建一个函数,该函数包含两个参数,n和high,并将n^1 n^2的值打印到n^high(在一行上)-任何帮助都将非常感谢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) + '
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
而countn**count
已经是一个int
。在将其添加到''
之前,需要将其转换为str
。这应该是new+=str(n**count)+'
在追加空格之前,需要将整数转换为字符串
new += str(int((n)**count)) + ' '
对于你的具体问题,你有一些答案。另外,与其编写一个函数来计算和打印功率,我建议您编写一个函数来计算功率并返回一个值列表:例如,[2,4,8,16,32]
。然后,您可以对该数据列表执行任何操作,包括将其发送到另一个函数以将它们连接到以空格分隔的字符串和/或打印。换句话说,计算和打印是非常不同的,所以要将它们分开。