+ 1

Color input

If I enter a color in advance in the program, it can become a number. If I enter a color while the program is running there will be an error. https://code.sololearn.com/cmjJFuk0vg2K/?ref=app

6th Dec 2020, 9:55 AM
Operand
Operand - avatar
2 Answers
+ 2
You need to provide a base 16 to the int function in order to convert the hex string "0xffffff" to an int. So in you first lines, it should be like: col = int(input(), 16)
6th Dec 2020, 10:15 AM
CHMD
CHMD - avatar
+ 1
In the first case the hex number is in string form (because input() returns string). So to convert that to integer, you need to pass in an extra argument to tell Python that this is a hex number in the form of a string, that you want to convert to int. That extra argument is the number 16, as the base of hex numbers is 16 col = int(input(), 16) #works fine In the second case, you are directly converting a hex number to int, so there is no problem.
6th Dec 2020, 10:19 AM
XXX
XXX - avatar