11 Replies - 751 Views - Last Post: 04 May 2014 - 06:12 AM Rate Topic: -----

#1 mojosnake  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-May 14

*noob* input() y/n , y is not defined

Posted 03 May 2014 - 12:52 PM

ok first off, hello all !! if your reading this this then you are either here to flame/troll or help the noob.
great, lets get started . let me begin by saying this is not schoolwork , this is a lazy Saturday on the couch experimenting with python and and trying to learn the basics.Mostly just trying to get a handle on for loops and if statements.So anyways i was just messing with a silly bit of code to demonstrate various concepts to myself and i came across an anomaly of sorts ( well to me anyway ) and an error that i cant seem to figure out .
ok lets look at the code:

def boom():    
    import time
    mylst= [1,2,3]
    y=str("boom!!!!!!!!!!!!!!!!!!!!!!!!!")

    print('hello this message self destructs in 3 seconds:')

    for anythngiwantcangohere in mylst:
        time.sleep(1)
        print(anythngiwantcangohere)

    for anything in y: 
        print (y)
        
boom()

ans = input('would you like to boom again? y/n')

if ans == 'y':
    boom()
else:
    print('goodbye')



ok so the function works as expected but does contain the anomaly that 'boom!!!!!!!!!!!' is print on the screen ALOT of times. I'm pretty sure i dont understand what im doing here:

 for anything in y: 
        print (y)


ok thats the first bit , the error comes into play on line 17
i cant seem to get it to accept " y "as input

the error given is:
Traceback (most recent call last):
File "/home/mojo/test.py", line 17, in <module>
ans = input('would you like to boom again? y/n')
File "<string>", line 1, in <module>
NameError: name 'y' is not defined

so thats where im stuck at , any hints tips or tricks ???????

Is This A Good Question/Topic? 0
  • +

Replies To: *noob* input() y/n , y is not defined

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,982
  • Joined: 03-December 12

Re: *noob* input() y/n , y is not defined

Posted 03 May 2014 - 01:18 PM

Problem: Boom!!!... prints several times

Using a for loop causes the loop to go over each character and print that number of times,
>>> print(len('boom!!!!!!!!!!!!!!!!!!!!!!!!!'))
29


So it loops 28 times, (0 based to array size - 1)

Problem: NameError: name 'y' is not defined

	for anything in y:
	       print (y)



What are you expecting here? y is not defined as the error tells you.

rename each item in y, anything
print the original container(y)

What are you expecting to be in y?

This post has been edited by astonecipher: 03 May 2014 - 01:19 PM

Was This Post Helpful? 2
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7760
  • View blog
  • Posts: 13,124
  • Joined: 19-March 11

Re: *noob* input() y/n , y is not defined

Posted 03 May 2014 - 01:33 PM

View Postastonecipher, on 03 May 2014 - 03:18 PM, said:

Problem: Boom!!!... prints several times

Using a for loop causes the loop to go over each character and print that number of times,
>>> print(len('boom!!!!!!!!!!!!!!!!!!!!!!!!!'))
29


So it loops 28 times, (0 based to array size - 1)


Well, 29 times (once for each char in y), but yes, this is correct apart from that.

Quote

Quote

Problem: NameError: name 'y' is not defined

	for anything in y:
	       print (y)



What are you expecting here? y is not defined as the error tells you.


rename each item in y, anything
print the original container(y)

What are you expecting to be in y?



This is not correct. y was defined at the top of the method, so it's defined at this line. It's not defined in the top level, though, and this is the problem.
The issue is that the input function means two different things, depending on whether you're using python 2 or python 3. In 3, it means what you expect it to mean: get input and return it as a string. In python 2, however, it means "get input and try to evaluate the string as the name of a python object"

So:

>>> #y undefined to start
... 
>>> user_response = input ("do it? ")
do it? y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'y' is not defined
>>> y = "some string that I didn't type in response to the question"
>>> user_response = input ("do it? ")
do it? y
>>> user_response
"some string that I didn't type in response to the question"


In python2, the correct function to use for this purpose is raw_input:

>>> user_response = raw_input ("do it? ")
do it? y
>>> user_response
'y'

This post has been edited by jon.kiparsky: 03 May 2014 - 01:34 PM

Was This Post Helpful? 2
  • +
  • -

#4 mojosnake  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-May 14

Re: *noob* input() y/n , y is not defined

Posted 03 May 2014 - 02:00 PM

first off let me thank both of you for your replys. they both proved helpful.
astonecipher - our explanation helped understand what I did with the for loop ,I totally get it.
jon.kirparsky - I'm stumped....lol . Im trying to ask for input y/n to start the function boom () again , if "y" then boom() else "goodbye" im probably missing the obvious here
Was This Post Helpful? 0
  • +
  • -

#5 mojosnake  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-May 14

Re: *noob* input() y/n , y is not defined

Posted 03 May 2014 - 02:07 PM

ok the obvious was that i needed to use raw_input .
it now acecpt y as input and run the fucntion boom () again
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7760
  • View blog
  • Posts: 13,124
  • Joined: 19-March 11

Re: *noob* input() y/n , y is not defined

Posted 03 May 2014 - 02:19 PM

The key thing that you might be missing is that in python, functions are objects. Keep that in mind and walk through this (try the code in the interpreter, too - that's really important)

>>> def y():
...     print "the user entered 'y'!"
...
>>> type (y)
<type 'function'> 



So here I've defined a function called y. y is the name of the function, just as x might be the name of an int or a str.

>>> user_response = input ("Do it? ")
Do it? y
>>> type (user_response)
<type 'function'>
>>> user_response == 'y'
False
>>> y == 'y'
False
>>> y == user_response
True



So now I get the response from the user, and I assign it to the variable named user_response. The question is, what have I got here? As you can see, what I've got is something that is a function, and it's the same thing as y, which we defined above. It's not the same thing as the string 'y'! This is critical to understand: the function called y != the string 'y'.



Finally, let's look at user_response:

>>> user_response()
<function y at 0x10bd72668>
>>> user_response()
the user entered 'y'!


So this thing is a function, and it does the same thing as y:

>>> y
<function y at 0x10bd72668>
>>> y()
the user entered 'y'!


Notice that it even has the same address on the heap - to python, this means that user_response is exactly the same thing as y, the two are just different names pointing at the same item. (In programming, this is called "aliasing" and it's an interesting source of both useful effects and bugs)


I hope this clears things up. Go into the interpreter and play around with these examples a little if you need to -that's usually the best way to understand these things.

View Postmojosnake, on 03 May 2014 - 04:07 PM, said:

ok the obvious was that i needed to use raw_input .
it now acecpt y as input and run the fucntion boom () again



Yep. The last thing I'll add about this is that in python 3, there is no raw_input() function, and input() works as you expect it to: it returns the string that the user gave you. And you were right to be confused here - the behavior of the input() function in python2 is just wrong, and it's a standard "gotcha". Which is why it was changed in python 3

This post has been edited by jon.kiparsky: 03 May 2014 - 02:17 PM

Was This Post Helpful? 3
  • +
  • -

#7 mojosnake  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-May 14

Re: *noob* input() y/n , y is not defined

Posted 03 May 2014 - 02:25 PM

thank you very very much jon . your info is very valuable and will become a further source of study , the fact that i even understand a fraction shows i might get it lol . If you look at my code I changed the one instance of input () to raw input () and it all works as expected now . in after each my next quest in this is to get it to keep iterating the question to continue again after each boom () . Im thinking a while loop and i even tried one but it didnt work out so well .
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7760
  • View blog
  • Posts: 13,124
  • Joined: 19-March 11

Re: *noob* input() y/n , y is not defined

Posted 03 May 2014 - 02:32 PM

Keep trying - you'll find it! (and yes, a while loop is a correct way to do this.)
Was This Post Helpful? 1
  • +
  • -

#9 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,982
  • Joined: 03-December 12

Re: *noob* input() y/n , y is not defined

Posted 03 May 2014 - 02:59 PM

I will say Jon is the most helpful if not most knowledgeable in the Python forum and is inspirational with his teaching.
Was This Post Helpful? 1
  • +
  • -

#10 mojosnake  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-May 14

Re: *noob* input() y/n , y is not defined

Posted 03 May 2014 - 03:43 PM

well jon your a right I did indeed find a way. I did end up using a while loop with (i think ) a boolean test? Everything now works as i expected
the program goes boom() and asks the user if it want to boom() again until anything other than y is typed and then says good bye and exits . small programming achievement I know. somehow though i feel I built some sort of foundation . I believe I used the full set of "primitives" , maybe lol anyways heres the updated working source

def boom():    
    import time
    mylst= [1,2,3]
    y=str("boom!!!!!!!!!!!!!!!!!!!!!!!!!")

    print('hello this message self destructs in 3 seconds:')

    for anythngiwantcangohere in mylst:
        time.sleep(1)
        print(anythngiwantcangohere)

    for anything in y: 
        print (y)
        
boom()

while True:
    ans=raw_input('would you like to boom again? y/n')
    if (ans == 'y'):
        boom()
    else:
        print('goodbye')
        break



astonecipher you helped me great deal also fine sir
Was This Post Helpful? 1
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7760
  • View blog
  • Posts: 13,124
  • Joined: 19-March 11

Re: *noob* input() y/n , y is not defined

Posted 03 May 2014 - 07:01 PM

Yes, a loop with a break is one good pattern for this. Some people object to this, though, finding it hard to follow when the code gets complex. (I'm not one of those people, but you will run into them)
Another approach would be something like

keep_going=True
while (keep_going)
  prompt user
  if yes, do the thing
  if no, set keep_going to False
  otherwise, bad input, so do nothing and prompt the user again


This post has been edited by jon.kiparsky: 03 May 2014 - 07:02 PM

Was This Post Helpful? 1
  • +
  • -

#12 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 124
  • Joined: 08-May 11

Re: *noob* input() y/n , y is not defined

Posted 04 May 2014 - 06:12 AM

View Postjon.kiparsky, on 03 May 2014 - 09:01 PM, said:

Yes, a loop with a break is one good pattern for this. Some people object to this, though, finding it hard to follow when the code gets complex. (I'm not one of those people, but you will run into them)
Another approach would be something like

keep_going=True
while (keep_going)
  prompt user
  if yes, do the thing
  if no, set keep_going to False
  otherwise, bad input, so do nothing and prompt the user again


Another way to do it would be to use the exit() command instead of break. Not sure if the same people who object to using break object to using exit as well though. As you can see there are several different ways to accomplish the same task here. You'll realize that this happens a lot in programming. Maybe one way is faster than another or uses less commands. I've (and I think most people) generally favor the faster running code to be better.

In this case it wouldn't show a noticeable difference in run time but as your prorgrams grow in complexity you'll most likely want to use the faster running code. You can use timeit to test the speed of things in your programs. One thing that will speed up things (again not really noticably to humans) would be to type from time import sleep instead of importing the whole time library.

This post has been edited by alexr1090: 04 May 2014 - 06:13 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1