pythonpython-unicode

Python print radical sign in though getting type error about not being able to typecast bytes


Here is the error I'm receiving:

img

The code I am using in repl.it is the same one I wrote in Techsmart ((a browser based IDE for my class)( and worked perfectly:

import math
square = []
perfect_squares = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225]
num = int(input(""))
for i in perfect_squares:
    if num % i == 0:
        square.append(i)
coefficient = max(square)
radicand = num // coefficient
coefficient = int(math.sqrt(coefficient))
if num in perfect_squares:
    print(int(math.sqrt(num)))
elif coefficient == 1:
    print(u"\u221A".encode('utf-8'), num, sep='')
else:
    print(coefficient, u"\u221A".encode('utf-8'), radicand, sep='')

Solution

  • You might correct the encoding of the radical symbol in this line print(coefficient, u"\u221A".encode('utf-8'), radicand, sep='') as follows.

    import math
    square = []
    perfect_squares = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225]
    num = int(input("Enter a number: "))
    for i in perfect_squares:
        if num % i == 0:
            square.append(i)
    coefficient = max(square)
    radicand = num // coefficient
    coefficient = int(math.sqrt(coefficient))
    if num in perfect_squares:
        print(int(math.sqrt(num)))
    else:
        print(coefficient, "\u221A", radicand, sep='')