英文:
Python print radical sign in though getting type error about not being able to typecast bytes
问题
这是我收到的错误消息:
在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:
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='')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论