1

I made a program in python that is supposed to accept a name as user input. It will then check if the name given is contained inside a string that is already given and if it is then the program will print out the telephone next to that name. My code is as follows:

tilefwnikos_katalogos = "Christoforos 99111111: Eirini 99556677: Costas 99222222: George 99333333: Panayiotis 99444444: Katerina 96543217"
check=str(input("Give a name: "))
for check in tilefwnikos_katalogos:
  if check=="Christoforos":
    arxi=check.find("Christoforos")
  elif check=="Eirini":
    arxi=check.find("Eirini")
  elif check=="Costas":
    arxi=check.find("Costas")
  elif check=="George":
    arxi=check.find("George")
  elif check=="Panayiotis":
    arxi=check.find("Panayiotis")
  elif check=="Katerina":
    arxi=check.find("Katerina")
  s=check.find(" ",arxi)
  arxi=s
  y=check.find(":",arxi)
  telos=y
apotelesma=tilefwnikos_katalogos[arxi+1:telos]
print(apotelesma)

But when I try to run it, I input the name and then the following message pops up:

Traceback (most recent call last):

File "C:\Users\Sotiris\Desktop\test.py", line 16, in <module> s=check.find(" ",arxi)

NameError: name 'arxi' is not defined

What am I doing wrong?

jmetz
  • 12,144
  • 3
  • 30
  • 41

2 Answers2

0

You're getting your error because arxi isn't getting defined in the first place when then name the user gave is not present on your list.You can fix that by simply adding an unconditional else case to your if/else if bundle as pointed in the comments. But the very way you tackled this problem is faulty, storing data like this in a string is a bad idea, you want to use a dictionary:

phone_catalog = {'Christoforos': 99111111, 'Eirini': 99556677, 'Costas': 99222222, 'George':99333333, 'Panayiotis':99444444, 'Katerina': 96543217}

Also check isn't a very clear variable name, maybe you should try using something better like:

user_name = str(input("Give a name: "))

And now you can do your if/elif condition but replacing it for using dictionary logic and making sure you have a final else, like such:

if user_name in phone_catalog:
    print(phone_catalog[user_name])
else:
    print("Unknown user")

See how the dictionary made your life much easier and your code cleaner here? Read more on Python Data Structures.

Bernardo Meurer
  • 2,295
  • 5
  • 31
  • 52
  • 4
    I would probably add a note about why they were getting the error they got in the first place, just to at least directly address the OP original concern. Ultimately, this does not address the actual question the OP had. – idjaw Mar 18 '16 at 15:19
  • @Noelkd No, which does not imply code improvements can't be pointed towards. OP is using a totally kaput data structure here, teaching him about dictionaries does no harm – Bernardo Meurer Mar 18 '16 at 15:23
  • 3
    I just can't understand why you would brush over the actual problem rather than deal with that first and then offer improvements – Noelkd Mar 18 '16 at 15:25
  • @Noelkd I saw that fault and edited the question to provide just that, also, his problem had already been pointed out in the comments. – Bernardo Meurer Mar 18 '16 at 16:00
0

so there are a few things you have overlooked / not going as expected, the first of which is how iterating over strings in python works:

tilefwnikos_katalogos = "Christoforos 99111111: Eirini 99556677: Costas 99222222: George 99333333: Panayiotis 99444444: Katerina 96543217"
for check in tilefwnikos_katalogos:
    print(check)
    #print(repr(check)) #this shows it as you would write it in code ('HI' instead of just HI)

so check can never be equal to any of the things you are checking it against, and without an else statement the variable arxi is never defined. I'm assuming you meant to use the check from the user input instead of the one in the loop but I'm not sure you need the loop at all:

tilefwnikos_katalogos = "Christoforos 99111111: Eirini 99556677: Costas 99222222: George 99333333: Panayiotis 99444444: Katerina 96543217"
check=str(input("Give a name: ")) #the str() isn't really necessary, it is already a str.

if check=="Christoforos":
    arxi=check.find("Christoforos")
elif check=="Eirini":
    arxi=check.find("Eirini")
elif check=="Costas":
    arxi=check.find("Costas")
elif check=="George":
    arxi=check.find("George")
elif check=="Panayiotis":
    arxi=check.find("Panayiotis")
elif check=="Katerina":
    arxi=check.find("Katerina")
else: raise NotImplementedError("need a case where input is invalid")
s=check.find(" ",arxi)
arxi=s
y=check.find(":",arxi)
telos=y
apotelesma=tilefwnikos_katalogos[arxi+1:telos]
print(apotelesma)

but you could also just see if check is a substring of tilefwnikos_katalogos and deal with other conditions:

if check.isalpha() and check in tilefwnikos_katalogos:
    #    ^                  ^ see if check is within the string
    #    ^ make sure the input is all letters, don't want to accept number as input
    arxi=check.find(check)
else:
    raise NotImplementedError("need a case where input is invalid")

although this would make an input of C and t give Cristoforos' number since it retrieves the first occurrence of the letter. An alternative approach which includes the loop (but not calling the variable check!) would be to split up the string into a list:

tilefwnikos_katalogos = "..."
check = input(...)
for entry in tilefwnikos_katalogos.split(":"):
    name, number = entry.strip().split(" ")
    if check == name:
        apotelesma=number
        break
else:
    raise NotImplementedError("need a case where input is invalid")

although if you are going to parse the string anyway and you may use the data more then once it would be even better to pack the data into a dict like @BernardMeurer suggested:

data = {}
for entry in tilefwnikos_katalogos.split(":"):
    name, number = entry.strip().split(" ")
    data[name] = number #maybe use int(number)?

if check in data:
    apotelesma = data[check]
else:
    raise NotImplementedError("need a case where input is invalid")
Community
  • 1
  • 1
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59