2
\$\begingroup\$

In this library:

  1. text_data(val) translates val to computer data types
  2. colorcode() returns the selected color in hex

Can you please tell me how I can make it better?

from tkinter.colorchooser import askcolor
def text_data(text):
 texti = []
 binary = []
 ordv = []
 hexadecimal = []
 octal = []
 i = 0
 while i < len(text):
 j = ord(text[i])
 k = bin(ord(text[i]))
 l = hex(ord(text[i]))
 m = oct(ord(text[i]))
 k = k[2:]
 l = l[2:]
 m = m[2:]
 print(text[i], " ", j, " ", k, "", l, "", m)
 binary.append(k)
 ordv.append(j)
 hexadecimal.append(l)
 texti.append(text[i])
 octal.append(m)
 i += 1
 print("\n")
 print(binary)
 print("\n")
 print(ordv)
 print("\n")
 print(hexadecimal)
 print("\n")
 print(octal) 
def colorcode():
 return askcolor()[1]
print(colorcode())
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 28, 2017 at 21:52
\$\endgroup\$
2
  • \$\begingroup\$ Is this finished, working code? What is the intended usage of text_data(), and could you provide an example? \$\endgroup\$ Commented Aug 29, 2017 at 18:12
  • \$\begingroup\$ Are you the same person as Bloxy Craft? If so, would you like to have your accounts merged? \$\endgroup\$ Commented Aug 29, 2017 at 21:12

2 Answers 2

4
\$\begingroup\$

Instead of

i = 0
while i < len(text):
 j = ord(text[i])
 ...
 ...
 ...
 i += 1

and using text[i] for accessing individual elements (which is NOT a Pythonic way).
Get rid of i and write simply

for c in text:
 j = ord(c)
 ...
 ...
 ...

 j = ord(text[i])
 k = bin(ord(text[i]))
 l = hex(ord(text[i]))
 m = oct(ord(text[i]))

After performing the first of those commands is ord(text[i]) already in the variable j, isn't it? So why not reuse it in the rest of them:

for c in text:
 j = ord(c)
 k = bin(j)
 l = hex(j)
 m = oct(j)

Instead of

print(text[i], " ", j, " ", k, "", l, "", m)

use the format() method and replacement fields {}:

print("{} {} {} {} {}".format(text[i], j, k, l, m))
200_success
146k22 gold badges190 silver badges479 bronze badges
answered Aug 29, 2017 at 0:02
\$\endgroup\$
1
\$\begingroup\$

What about following the PEP 8 -- Style Guide for Python Code?
This style guide is about consistency. Consistency with this style guide is important.

Your code will become

from tkinter.colorchooser import askcolor
def text_data(text):
 texti = []
 binary = []
 ordv = []
 hexadecimal = []
 octal = []
 i = 0
 while i < len(text):
 j = ord(text[i])
 k = bin(ord(text[i]))
 l = hex(ord(text[i]))
 m = oct(ord(text[i]))
 k = k[2:]
 l = l[2:]
 m = m[2:]
 print(text[i], " ", j, " ", k, "", l, "", m)
 binary.append(k)
 ordv.append(j)
 hexadecimal.append(l)
 texti.append(text[i])
 octal.append(m)
 i += 1
 print("\n")
 print(binary)
 print("\n")
 print(ordv)
 print("\n")
 print(hexadecimal)
 print("\n")
 print(octal)
def colorcode():
 return askcolor()[1]
print(colorcode())

Note particularly surrounding your function definitions with 2 blank lines.

(You may check it online: PEP8 online - as your original code, as this one.)

answered Aug 28, 2017 at 23:53
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.