Python 打印根号,但出现类型错误,无法将字节进行类型转换。

huangapple go评论67阅读模式
英文:

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

问题

这是我收到的错误消息:

Python 打印根号,但出现类型错误,无法将字节进行类型转换。

在repl.it中使用的代码与我在Techsmart中编写的代码相同(Techsmart是我课程中的基于浏览器的IDE),并且完美运行:

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='')
英文:

Here is the error I'm receiving:

Python 打印根号,但出现类型错误,无法将字节进行类型转换。

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='')

答案1

得分: 0

您可以在这行代码中更正根号符号的编码,如下所示。

print(coefficient, "√".encode('utf-8'), radicand, sep='')
英文:

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='')

huangapple
  • 本文由 发表于 2023年2月10日 05:43:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定