Ask coding questions

← Back to all posts
I am new could someone help me fix my problem.
AbdulRahmanAl13 (0)

MY code:

print('Robot: Hi my name is "Robot" Your Emotional Helping Hand "EHH" for short.')
print("Robot: How is your day? Good or Bad?")
Feeling=input("You: ")
if Feeling == "Bad":
print("Robot: I am sorry to hear that.")
print("Robot: How can I help? ")
print("-------------------------------------------")
print("If Option 1: GO AWAY! Type: 1 ")
print("If Option 2: I could real use a friend to talk to. Type: 2 ")
print("Pick Only One")
print("-------------------------------------------")
Options = input("Option: ")
elif Feeling == "Good":
print("Robot: Good Have a nice day:) shuting down")
if Options == "1":
print("You: GO AWAY!")
print("Robot: ok:( shuting down")
elif Options == "2":
print("You: I could real use a friend to talk to.")
print("Robot: Ok, What do you want to talk about? ")

The Problem:
Robot: Hi my name is "Robot" Your Emotional Helping Hand "EHH" for short.
Robot: How is your day? Good or Bad?
You: Good
Robot: Good Have a nice day:) shuting down
Traceback (most recent call last):
File "main.py", line 16, in <module>
if Options == "1":
NameError: name 'Options' is not defined

Answered by notGilbert (73) [earned 5 cycles]
View Answer
Comments
hotnewtop
notGilbert (73)

It's kinda hard to read because of the improper indenting, but if the Options = input("...") line is in the scope of an if and you're trying to access it outside, there's going to be an error

how to fix:
add a global before "Options"

if condition:
  global Options = input('asdf')

if Options == '1':
  # do things
IMayBeMe (344)

The issue is that you need the option conditions inside the first if statement.
Here is the fixed code:

print('Robot: Hi my name is "Robot" Your Emotional Helping Hand "EHH" for short.')
print("Robot: How is your day? Good or Bad?")
Feeling = input("You: ")
if Feeling == "Bad":
    print("Robot: I am sorry to hear that.")
    print("Robot: How can I help? ")
    print("-------------------------------------------")
    print("If Option 1: GO AWAY! Type: 1 ")
    print("If Option 2: I could real use a friend to talk to. Type: 2 ")
    print("Pick Only One")
    print("-------------------------------------------")
    Options = input("Option: ")
    if Options == "1":
        print("You: GO AWAY!")
        print("Robot: ok:( shuting down")
    elif Options == "2":
        print("You: I could real use a friend to talk to.")
        print("Robot: Ok, What do you want to talk about? ")
elif Feeling == "Good":
    print("Robot: Good Have a nice day:) shuting down")
Bookie0 (5983)

Can you link a repl? It'll be easier to find the problem. Also with elif Feeling == "Good":, that won't work as you can't have an elif statement hanging on its own, you'll need an if statement before that.