Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

name is not defined error. Python

Hello, i have been trying to work on a dummy, system for a script i've been writing. the way it worked before was that it wanted a "yes" or a "no" to get a result, but i want to change it so that it ONLY accepts yes or no, and gives a different response if none are true. I have set the code up correctly for what i want, but i keep getting a name not defined error on line 17. the name is mood, (which is a variable i think). the revlevant lines are 16 to 22. I am so confused! link: https://w.trhou.se/v4i2k3thj1

Please take a snapshot of your code so it is much easier to read.

You can do this by going to the workspace and clicking the camera icon in the upper right. You then share the link and someone else can view what you have.

Let me know if you need anything else.

2 Answers

So I noticed a few things. Don't worry, I'll address your mood question.

  1. You kept adding print(" ") to add spaces between text in your code. An easier way to do this is using \n. Without going into details, \n will skip a line and \t will add a tab. This will save you a lot of empty print statements.

  2. You never spaced out your code. All your code was in one block with no spaces in-between which made it hard to realize your mood question was stuck in an if statement. I have moved it out for you.

  3. When you use input, it will return a string. Your mood variable was a string so you got errors saying you can't compare strings and ints. I have cast your mood as an int so you can compare it to an int without getting the error.

  4. You use if statements when you should be using elif or else statements. This may be because you are new, but an elif will run if the original if statement does not run and the elif condition is true. An else statement will run if the if or elif is not true.

Overall, study what I have changed as it will help you in future scripts. I like this a lot and wish you luck in your future endeavors. I apologize if I have changed too much, I wanted to change a few major things to help you out, but not change too much that it is no longer your code.

import sys
sys.tracebacklimit = 0

print("Hello, I am an intelligent Python script!\n")
first_name = input("What is your first name?  ")
confirm_name = input("\nAre you absolutely sure your name is {}?  Yes/No  ".format(first_name))

# You used 2 if statements when an if/else would be more effective. There was no scenario in which both if's ran so use an if/else.
if confirm_name == "Yes":
    print("Very good then.")
else:
    print("User not confirmed,")
    print("\n\n--- Connection Terminated ---\n\n")
    exit()

while(True):
  try:
    # Your mood question was stuck inside of an if statement. 
    # input will always return a string so you have to cast it as an int
    mood = int(input("On a scale of 1 - 10, how is your day going, {}?   ".format(first_name)))

    if (mood < 1):
      raise TypeError("Please type in a number from 1 to 10")
    elif (mood > 10):
      raise TypeError("Please type in a number from 1 to 10")

  except ValueError as err:
    print("Please type in a number from 1 to 10")
  except TypeError as err:
    print(err)
  else:
    break

if mood == list(range(1, 10)):
    print("That is not a valid number!")
    exit()
elif mood >= int("6"):
    print("Ah, {} is pretty alright!".format(mood))
    print("\nPersonally, I'm an 11! This day has been rather...electrifying!\n")       
elif mood <= int("5"):
    print("Well, I hope this conversation has improved your {} to something better. I must add that I am fairly entertaining.\n".format(mood))
    assistance = input("Have I helped your mood today?  Yes/No ")

    if assistance == "Yes":
        print("\nI'm glad I could help you out today {}!\n".format(first_name))
    else:
        print("\nI'm very sorry to hear that.\n")

print("Unfortunately, I have to go now... Take care and have a good rest of your day!\n")
print("From,\n\t\t Super cool Python script by Austin")
print("\n\n--- Connection Lost to Server ---\n\n")

# You do not need to exit if it is the end of your code and not inside of a loop.

I do not want anyone to be able to enter something like "100", or "t" for example, only a value of 1 to 10, so now I do not know how to test for a value between 1 and 10 while spitting out any bad inputs.

I have updated my answer so mood will only accept numbers 1 to 10

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

I see two things wrong with your code.

1 - line 16 - the mood declaration is indented under the confirm name if and after the exit() so it won't be executed. Try pulling it back to the same level as the if on line 17.

2 - line 17 - you are setting mood to a string and then comparing it to an int() later on. Try surrounding the input on line 16 inside int().

Then it seems to work - with minimal testing

that will not work. I am trying to make it so that if someone doesn't input a number between 1 and 10, that it tells them thats a bad number. my dad wants to be able to put in a letter like "t", and have it stop the code.

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

aha - understood

I would then handle mood like below:

valid_moods = ", ".join(map(str, range(1, 10)))
if mood not in valid_moods:

You'll also need to wrap mood in int() below on the check for >=6 and <=5