1

OK, so I've been trying this for about 2 hours now and I can't seem to figure it out. I think I tried almost every possible algorithm combination and it still isn't working. Here it goes:

I'm trying to validate keyboard input in Python based on two condition (ordered by priority):

  1. Check if input is integer
  2. Check if input is vertex (class method for checking if the number given can be found as a key in a dictionary)

Here's the code:

def checkVertex(self, x):
    ok = 0
    for key in self.__inv:
        if key == x:
            ok += 1
            break
    for key in self.__outv:
        if key == x:
            ok += 1
            break
    if ok == 2:
        return True
    return False

def checkInt(number):
if number.isdigit() is False:
    return False
return True

def readVertex(msg, graf): <-- this is the issue
"""
msg - string message
graf - Graph() class instance initialised somewhere
invalid_input - string error message
"""
vertex = raw_input(msg)
while checkInt(vertex) is False:
    print invalid_input
    vertex = raw_input(msg)
    if checkInt(vertex) is True:
        vertex = int(vertex)
        if graf.checkVertex(vertex) is True: <-- this bloody line is not working right
            return vertex
        else:
            continue
return int(vertex)

source = readVertex("Give source vertex (by number): ", G)
dest = readVertex("Give destination vertex (by number): ", G)
cost = int(raw_input("Give cost: "))
print G.addEdge(source, dest, cost)

The problem that I'm getting is that the first condition works, so if I input a letter it will print an error, but if I input a number and that number isn't in the keys of the dictionary it will still return it.

So graf.checkVertex(vertex) always returns True in the above code even though I know for a fact that it works because I've tried the function with the same input in the shell and it returned False.

Let me give you an example, let's say I have this dict:

{0: [], 1: [], 2: [], 3: [], 4: []}

Screen recording of example:

enter image description here

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
serge
  • 126
  • 3
  • 13

1 Answers1

0

Your validation only runs while checkInt(vertex) is False: - if it is a valid integer the first time, you never check the rest. It's not that graf.checkVertex(vertex) doesn't work; it's never called. Instead, try:

def readVertex(msg, graf, invalid_input):
    """
    msg - string message
    graf - Graph() class instance initialised somewhere
    invalid_input - string error message
    """
    while True:
        vertex = raw_input(msg)
        if checkInt(vertex) and graf.checkVertex(int(vertex)):
            return int(vertex)
        print invalid_input

or

def readVertex(msg, graf, invalid_input):
    """
    msg - string message
    graf - Graph() class instance initialised somewhere
    invalid_input - string error message
    """
    while True:
        try:
            vertex = int(raw_input(msg))
        except ValueError:
            print invalid_input
        else:
            if graf.checkVertex(vertex):
                return vertex
            print invalid_input
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Thanks a lot! I only now realise how stupid I've been. Quick question though: when I used int(vertex) in graf.checkVertex(int(vertex)) it would always give me a ValueError when entering a letter, but now it doesn't. And if it does work now, doesn't it convert the letter into it's ASCII code? eg: int("a") would be 32 or whatever "a" does have for its code. I'm sorry if I don't make sense but my braind is f*cked up right now, thanks. – serge Apr 29 '14 at 15:03
  • No problem - there's a good community wiki on taking user input [here](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-he-gives-a-valid-response/23294659#23294659) – jonrsharpe Apr 29 '14 at 15:07
  • @csergiu_ro `and` is lazy - the second part will only run if the first part is `True`, so you only attempt to `int(vertex)` if `checkInt(vertex) == True`. – jonrsharpe Apr 29 '14 at 15:08