Python dictionaries

I’m working on a Caesar cipher program in Python
this is the main idea:

I made a function that takes two parameters - (message , shift key)
message1 = message is equal to users input to lower case
shift key1 = shift key is equal users input to integer
breaks up the message in to letters

for each letter if letter is in list of letters and values get value of letter
add shift key to the value get the letter for the new value and append it to the return message
else append the letter to the list

highlighted lines are code I don’t know how to deal with
how do I get user input on python?
how do I get a key in the dictionary by it’s value?

You don’t. Maybe you mean to say that you’ve got them backwards and want the other value as the key.
Unless you’re searching through the whole structure but then what’s the point.

I meant to say I’ve got the value for a key and I want to append that key to a list
how do I get the key from the dictionary using the value?

If you got the value by looking up a key, then you already have the key

If you want to do lookup in the other direction make a dictionary with the pairs flipped

If you want to search through the whole thing, then loop through all the pairs

I know I can get the value for a key by doing this:

dictionary.get(key)

but can I do it opposite?
cause I’ve got a value and then I add a shift key to it and now I need to append the letter for the new value to the list and need to get it from the list by the value
and I don’t really feel like making a new dictionary :wink:

The whole point is that the key dictates the location of the value, that’s why having a key lets you find the corresponding value

the value says nothing about the location

why would it be bad to have multiple dictionaries, if anything, it’s quite common to do transformation by creating a new structure based on another one as part of some computation. quite often getting from where you are to where you want to go is going to involve re-shaping the data until it suits the operations you want to be making on it

o.k. I guess I’d better be writing a new dictionary than.
How about getting user input?
I’ve tried raw_input but the coding environment I’m using doesn’t seem to recognize it I’m using repl.it python

writing? you’d make a copy, you wouldn’t duplicate the data source if that’s what you mean
you’d take the pairs…flip them… then put them into a dict

what about user input? that seems like something completely different

python3 renamed raw_input to input, and old input got removed, with the reason that input did something very different from what new users would expect, and made it too easy to do something silly that might seem like a good idea but isn’t

I know it’s something different I actually asked for help in it to on the top

There’s a file object at sys.stdin that you can read from, if you for example want to read all input, you would do:

import sys

sys.stdin.read()

file objects can read one line at a time too.

what you’d do is look up documentation for a file object and then consider what of the things that it offers is useful to you for what you want to do, you can also use the function dir to write out what attributes an object has:

Python 3.8.0 (default, Oct 23 2019, 18:51:26) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> dir(sys.stdin)
['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'write_through', 'writelines']
>>> 

there’s also a help function for writing out the docstring associated with objects, if there is one:

>>> help(sys.stdin.readline)
Help on built-in function readline:

readline(size=-1, /) method of _io.TextIOWrapper instance
    Read until newline or EOF.
    
    Returns an empty string if EOF is hit immediately.

input is nothing more than a shortcut for accessing sys.stdin, a beginner friendly feature

(file might sound wrong to you, but that is what user input looks to a program, just another file)

>>> help(input)
Help on built-in function input in module builtins:

input(prompt=None, /)
    Read a string from standard input.  The trailing newline is stripped.
    
    The prompt string, if given, is printed to standard output without a
    trailing newline before reading input.
    
    If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
    On *nix systems, readline is used if available.

why was raw_input removed?

It wasn’t. It was renamed.

also, that’s very googleable: “python how to read user input”
or “python raw_input is not defined”

oh I didn’t notice that :stuck_out_tongue_winking_eye:

Here’s how you might make a lookup-table for each direction:

>>> from string import ascii_lowercase
>>> 
>>> ctoi = {key: value for value, key in enumerate(ascii_lowercase)}
>>> itoc = ascii_lowercase
>>> ctoi['a']
0
>>> itoc[0]
'a'

ascii_lowercase is of course already a lookup-table by virtue of being indexable

You could also represent these as functions, since functions are mappings from input to output

1 Like

Another way to create that table:

ctoi = dict(zip(ascii_lowercase, count()))

See what’s going on there? Pair each letter with numbers counting up from 0 (that’s what itertools.count does)

Since those are now pairs of char-int, you can send them directly to the dict type to get a dict from that.

See why I protest a bit against “writing” a dict? (sounds like you want to hard-code each mapping manually) It’s more about reshaping the data you already have.

You could treat stdin the same way, think of a console program like a pipe connecting stdin to stdout, with some transformations happening along the way.

1 Like

I’m gonna read all that later it looks very helpful thanks! :+1:
I’m getting stuck with my project so I can’t read it right now
can you help? I’m getting this error I don’t understand

Traceback (most recent call last):  File "main.py", line 22, in <module>
    caesar()
  File "main.py", line 20, in caesar
    message_final=''.join(return_message)
TypeError: sequence item 16: expected str instance, NoneType found

do you know what it means?

Idk, have you read it? Because it reads almost like English. Really.

the 16th (or perhaps 17th) thing was supposed to be a string, but it was None

plus the error message tells you where in your code it was, giving you context

I read it and i understand just about what it means
It’s expecting a string but can’t find it,
but I can’t see why.
would you like to see the code?

The error message tells you which operation, and what sequences do you have there? Only one, right? And it says something about what’s in the sequence, do you agree that this sequence should only have strings considering what you’re doing with it? And that there therefore shouldn’t be a None? You could look at what’s in the sequence and see if there is a None in it.

what do you mean by sequence?