2

Currently, I am taking some computer science lessons online. I have been issued the task of writing a program that, upon a valid input, will print the next letter in the alphabet. Here is my code:

char = input()
ordchar = ord(char)
nextletter = ordchar + 1
nextletter = chr(nextletter)

if nextletter == ("["):
    print("A")

if nextletter == ("{"):
    print("a")

if nextletter!=("[","{"):
    print(nextletter)

Now, I do see the problem with the code, though I don't know how to rectify it. Basically, on the second to last line of code, I want it to essentially say "if nextletter does not equal { or [ print nextletter".

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2080719
  • 73
  • 1
  • 6

5 Answers5

5
if nextletter not in ["[", "{"]:
    print(nextletter)
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
  • downvoter: Please explain how my answer is wrong/different than the other answers? What? – Inbar Rose Feb 17 '13 at 15:53
  • @cIph3r: No, Ingbar is using a *list*, not a list comprehension. There is not much difference between using a tuple and a list here. – Martijn Pieters Feb 17 '13 at 15:55
  • @cIph3r You are correct that generators are faster than list-comprehension when storing the results doesn't matter, but in this case, you are just totally confused, see other responses. – Inbar Rose Feb 17 '13 at 15:56
  • This is fine, though I think @MartijnPieters' answer using `not in` against a string is a slightly more Pythonic way of doing this -- it's also a bit more readable, as there's only 2 characters of punctuation instead of 7. – Ben Hoyt Feb 17 '13 at 22:29
5

You do not need to test for those characters at all. In the code before it you make sure that nextletter is never going to be either [ or {, if you just assign a different letter to nextletter there:

if nextletter == "[":
    nextletter = "A"
elif nextletter == "{":
    nextletter = 'a'

If you still need to make such a test, use the not in operator:

if nextletter not in '[{':

not in returns True if the left-hand operand is not present in the sequence on the right. I used a string with two characters in my example, which counts as a sequence:

>>> '[' in '[{'
True
>>> '0' in '[{':
False
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
3

Use not in membership operator:

if nextletter not in ("[","{"):
    print(nextletter)

Also note that, you can avoid the third test if you used if-elif construct rather than individual if's. That way, you can just put the 3rd part in an else block.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

While the other answers are correct, the right way of doing what you want is using elifand else:

if nextletter == ("[") :
    print("A")
elif nextletter == ("{") :
    print("a")
else :
    print(nextletter)
Jaime
  • 65,696
  • 17
  • 124
  • 159
  • 2
    I was literally two characters away from that in an earlier version of the program, but I forgot to use elif. Can't blame me, I've only been using Python for 5 days. – user2080719 Feb 17 '13 at 15:59
  • @cIph3r Because you are not doing a redundant check: if you reach the `else` then `nextletter` cannot be either `[` or `{`. – Jaime Feb 17 '13 at 16:01
  • ok, but the runtime will be close to unmeasurable faster. Also the code is longer and looks more complex, making the code less readable ;) – El Hocko Feb 17 '13 at 16:05
  • @cIph3r It is not longer, takes the exact same number of lines as the OP's code. It's biggest advantage is not execution speed, but minimizing the chances for errors and maintainability: if tomorrow you decide to check for `?` instead of `[`, you only need to edit one line, not two. And if you find using `else if` statements hurts readability, then you need to read more code. – Jaime Feb 17 '13 at 16:20
  • @cIph3r The most voted answer still requires handling the `[` and `{` cases separately, which means four more lines. – Jaime Feb 17 '13 at 17:20
0

The problem is that you are asking if nextletter is not equal to the tuple of ("[","{"). You wanted to ask if it is not equal to either "[" or "{". Therefore, you must ask, if nextletter != "[" and nextletter != "{" This will check if it is neither "[" nor "{". Hope this helps

erdekhayser
  • 6,537
  • 2
  • 37
  • 69
  • And that can be shortened to any one of the answers by [Martijn](http://stackoverflow.com/a/14922929/1561176), [Rohit](http://stackoverflow.com/a/14922919/1561176) or [Myself](http://stackoverflow.com/a/14922921/1561176) – Inbar Rose Feb 17 '13 at 15:58