0

I am working on an omegle simulator for fun, where it asks your age and if you're old enough asks you if you have kik. It works fine if your age is 16 or more but if you say any less than that, it comes up with an error. This is the code:

age = input("age?\n")
if age == "1":
    print ("Too young bby")
elif age == "2":
    print ("Too young bby")
elif age == "3":
    print ("Too young bby")
elif age == "4":
    print ("Too young bby")
elif age == "5":
    print ("Too young bby")
elif age == "6":
    print ("Too young bby")
elif age == "7":
    print ("Too young bby")
elif age == "8":
    print ("Too young bby")
elif age == "9":
    print ("Too young bby")
elif age == "10":
    print ("Too young bby")
elif age == "11":
    print ("Too young bby")
elif age == "12":
    print ("Too young bby")
elif age == "13":
    print ("Too young bby")
elif age == "14":
    print ("Too young bby")
elif age == "15":
    print ("Too young bby")
else:
    kik = input("Do you have kik?\n")
yes = "yes"
if kik == yes:
    print ("add me bby")
else:
    print ("bye")

The error that comes up is:

 Traceback (most recent call last):
  File "C:/Users/Public/Documents/python/omegle.py", line 36, in <module>
    if kik == yes:
 NameError: name 'kik' is not defined

Does anyone know how to fix it?

2
  • 11
    For heavens sake, please cast the age to an integer and just use if age < 16:. Commented Oct 27, 2014 at 17:54
  • You need more indentation; it's important in Python. Commented Oct 27, 2014 at 17:56

5 Answers 5

2

The problem is that you only set kik in this block:

else:
    kik = input("Do you have kik?\n")

If this block isn't reached, kik doesn't exist. An option is to set it before your if/elif blocks.


Additionally, you can make this much shorter:

kik = "no"
age = input("age?\n")
if int(age) < 16:
    print ("Too young bby")
else:
    kik = input("Do you have kik?\n")
yes = "yes"
if kik == yes:
    print ("add me bby")
else:
    print ("bye")
Sign up to request clarification or add additional context in comments.

Comments

2

There are a couple of things you should fix here. FIrst, store the age as a number using int():

age = int(input("age?\n"))

And then do a less than:

if(age < 16):
  print ("Too young bby")
else:
  kik = input("Do you have kik?\n")
  if kik == "yes":
    print ("add me bby")
  else:
    print ("bye")

Comments

1

Set kik to the default it should be outside your chain.

age = input("age?\n")
kik = "no" #assuming no is default
...

As it is in your code, it will only be defined if you hit else

Comments

1

The short answer is that kik is out of scope, and putting kik = "no" at the beginning of your program should get rid of that error.

But, here's a better way to do the whole thing:

age = int(input("age?\n"))
kik = "no"

if age < 16:
    print ("Too young bby")
else:
    kik = input("Do you have kik?\n")

if kik == yes:
    print ("add me bby")
else:
    print ("bye")

2 Comments

"The short answer is that yes needs to be in quotes". I don't know about that. The condition should be equally valid whether you use the string literal "yes" or the variable named yes.
Yeah, you're right. I thought the traceback was complaining about yes being out of scope, not kik. I updated my answer.
0

Andy's post was very helpful to me. There is one thing I would add. If you enter an integer less than 16 to the first question, you get both print ("Too young bby") and print ("bye"). The first output only is sufficient for my use, not print ("bye"). To achieve this you indent the second 'if' statement to the first 'else' statement. If you do not indent my code, you get "You are not eligible to vote in Ireland." twice. Here is my example;

Citizen = "No"
Yes = "Yes"
Age = int(input("What is your age?: \n"))
if (Age) < 18:
    print("You are not eligible to vote in Ireland.\n")
else:
    Citizen = input("Do you hold Irish Citizenship? Yes/No: \n")
    if Citizen == Yes:
        print("You are eligible to vote in Ireland.\n")
    else:
        print("You are not eligible to vote in Ireland.\n")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.