如何使用python制作整数到十六进制转换器?

问题描述

我目前正在尝试在python中将int转换为十六进制。我已经尝试运行我的代码,但是 继续给我这个错误

Traceback (most recent call last):

  File "C:\Users\Saumya\Documents\Coding\Python\hex.py",line 2,in <module>
    hex(num1)
TypeError: 'str' object cannot be interpreted as an integer

这是我的代码

num1 = input("Enter a Number to Convert to Hex:")
hex(num1)

请尽快给我答案。

解决方法

input返回一个字符串。您必须将其转换为整数:

hex(int(num1))