Python check individual characters?
#1
Hey GS -

I'm learning Python 3, and as a learning tool I'm trying to compile a program that runs through terminal that uses very basic python scripting.

This program allows you to enter in a cypher by adding in values based on letters(so a=b b=c, so on);

is there any way I can allow users to input a message in string format and then have it check on each individual character, then repeat the variable back based on the message? I will gladly provide the source code I have so far to anyone willing to help, but keep in mind it's very basic(which may make it easier to help).


Thanks, happy hacking Smile

Edit:

View the source code here:
https://github.com/anth-rax/CipherBuild/...ter/CiB.py
Reply
#2
You can do something VERY simple to loop through a string in Python..

Code:
for c in "string variable or value":
  do_something(c)

Also if you have an index you can treat strings as a slice like so

string[0] or string[0:2] to print ranges of characters
Reply
#3
(10-31-2016, 04:09 PM)NO-OP Wrote: You can do something VERY simple to loop through a string in Python..

Code:
for c in "string variable or value":
 do_something(c)

Also if you have an index you can treat strings as a slice like so

string[0] or string[0:2] to print ranges of characters

okay so; 

say I have this bit of code storing the value: 
Code:
e_a = input("Enter value for  a>")
how would I a:
assign that value to the string letter "A" 
and b:
how would I make it so this bit of code
Code:
e_input = input("Please enter a message to encrypt>")
//user enters: Hello
reflects the stores variables?
edit:
I've added the source code to the original post so you can see exactly what I'm talking about
Reply
#4
I would just use raw_input

Code:
input_string = raw_input("Type a string: ")
print("User entered " + input_string)

# loop through each character
print("Looping through string as array")
for c in input_string:
  print(c)

print("Looping through string with index")
for i in range(0, len(input_string)):
  print(input_string[i])

Also here's a little something to make your code a little more dynamic.

Currently you manually go through the whole alphabet. Takes up waaaay too much space and can be done better. What I suggest is an array (list) of characters (the alphabet) and loop through it to create a dictionary (keyed with alpha characters) with the stored value of the input.

So you get something like this..
Code:
╰─$ python test.py
Enter value for a> 1
Enter value for b> 2
Enter value for c> 3
Enter value for d> 4
{'a': '1', 'c': '3', 'b': '2', 'd': '4'}

But the only code is this...

Code:
alph = ['a', 'b', 'c', 'd'] # etc a-z
cipher = {}

# dynamically create prompts and variables
for c in alph:
  cipher[c] = raw_input("Enter value for " + c + "> ")

print(cipher)

So after gathering the input if you want to access let's say the value for d you would have something like cipher["d"] and that would hold the value. I would really learn more about python and datatypes it seems like you're limiting yourself to strings and ints, making your code very bulky, which is a bad practice.
Reply
#5
(10-31-2016, 05:38 PM)NO-OP Wrote: I would just use raw_input

Code:
input_string = raw_input("Type a string: ")
print("User entered " + input_string)

# loop through each character
print("Looping through string as array")
for c in input_string:
  print(c)

print("Looping through string with index")
for i in range(0, len(input_string)):
  print(input_string[i])

Also here's a little something to make your code a little more dynamic.

Currently you manually go through the whole alphabet. Takes up waaaay too much space and can be done better. What I suggest is an array (list) of characters (the alphabet) and loop through it to create a dictionary (keyed with alpha characters) with the stored value of the input.

So you get something like this..
Code:
╰─$ python test.py
Enter value for a> 1
Enter value for b> 2
Enter value for c> 3
Enter value for d> 4
{'a': '1', 'c': '3', 'b': '2', 'd': '4'}

But the only code is this...

Code:
alph = ['a', 'b', 'c', 'd'] # etc a-z
cipher = {}

# dynamically create prompts and variables
for c in alph:
  cipher[c] = raw_input("Enter value for " + c + "> ")

print(cipher)

So after gathering the input if you want to access let's say the value for d you would have something like cipher["d"] and that would hold the value. I would really learn more about python and datatypes it seems like you're limiting yourself to strings and ints, making your code very bulky, which is a bad practice.

Keyword being practice Big Grin

I'm mainly using this as a learning tool. I'm not trying to make it 100% right or 100% best-practice-based, for now I'm just trying to get it to work

But, I do very much appreciate your feedback. I'm gonna try to mess around with it a bit and see if I can get a few things going, but I am aware I need to learn A LOT more Tongue I'm still very very amateur. Just trying to compile my first working program, not make it up to perfect specifications Smile
(I do however understand that you're just telling me that it's bad practice, and believe me I appreciate it
Similar to the "Design Hell" page, which features LOADS of HTML basics, I'm using basics. This may be "Programming Hell" but if it works and it works right, I don't care what the code looks like(for now, and as long as I can write it and manipulateTongue)
)
Edit:
Also, I'm just basically manipulating variables. Very very basic.
Edit2:

I forgot to mention this earlier, raw_input outputs an unassigned variable error(NameError)
Edit 3:
I tried out the dynamic code with full a-z and so far so good! Just turned like 56+ lines of code into 6, dick Tongue thanks a lot! Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Python] Can't Post To Pastebin? DeepLogic 1 1,759 05-03-2020, 01:01 AM
Last Post: dropzone
  Random bitcoin private key generation in python illmanors 1 5,867 01-31-2019, 01:09 PM
Last Post: sleepy999