+ Post New Thread
Results 1 to 10 of 10
Raspberry Pi, Arduino & Other SBCs Thread, Really basic python help needed in Technical; We are hoping to use python in our new curriculum so I am trying to get familiar with it. I've ...
  1. #1
    Bev
    Bev is offline

    Join Date
    Oct 2009
    Posts
    157
    Thank Post
    13
    Thanked 5 Times in 5 Posts
    Rep Power
    11

    Really basic python help needed

    We are hoping to use python in our new curriculum so I am trying to get familiar with it.

    I've set up a text editor, and installed python 3.4 on my mac, and am working through a 'teach yourself python' course.

    and I've fallen at the second hurdle.

    The first program was

    print(“Hello, World”)

    That worked

    so I tried the 2nd program
    name = input("What is your name? ")
    print("Hello,",name)


    It looks straightforward enough

    but
    I always get this error.


    Last login: Mon May 12 14:42:38 on ttys001
    Bevs-Mac-mini-4:~ BevStephenson$ cd '/Users/BevStephenson/Documents/Python/' && '/usr/local/bin/pythonw' '/Users/BevStephenson/Documents/Python/Hello Dave2.py' && echo Exit status: $? && exit 1
    What is your name? Bev
    Traceback (most recent call last):
    File "/Users/BevStephenson/Documents/Python/Hello Dave2.py", line 1, in <module>
    name = input("What is your name? ")
    File "<string>", line 1, in <module>
    NameError: name 'Bev' is not defined
    Bevs-Mac-mini-4:Python BevStephenson$



    I've tried it on my mac, on another pc, and also on an online Python compiler and they are all the same.

    Im at a complete loss

  2. #2

    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Knightsbridge
    Posts
    5,619
    Thank Post
    1,231
    Thanked 776 Times in 673 Posts
    Rep Power
    235
    Quote Originally Posted by Bev View Post
    Code:
    name = input("What is your name? ")
    print("Hello,",name)
    Try:

    Code:
    name = input("What is your name? ")
    print "Hello, " + name

  3. #3
    Bev
    Bev is offline

    Join Date
    Oct 2009
    Posts
    157
    Thank Post
    13
    Thanked 5 Times in 5 Posts
    Rep Power
    11
    No, that didn't work at all.

    I've discovered that if I use IDLE and run the program within IDLE then it works, but if I run it in the Terminal then it doesn't.

    Not sure what that means probably something wrong with the way I have python setup

  4. #4
    Bev
    Bev is offline

    Join Date
    Oct 2009
    Posts
    157
    Thank Post
    13
    Thanked 5 Times in 5 Posts
    Rep Power
    11
    Screen Shot 2014-05-12 at 15.08.33.png

    I don't know if you can see, but the 2 windows at the top are running in IDLE

    and at the bottom I've just run the text file using the python launcher and it hasn't worked.

    - - - Updated - - -

    and I got your + name version to work with the addition of some brackets.

  5. #5

    bladedanny's Avatar
    Join Date
    May 2009
    Location
    Sheffield
    Posts
    1,268
    Thank Post
    188
    Thanked 298 Times in 224 Posts
    Rep Power
    130
    Do you have python 2.7.6 installed as well?

  6. #6

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,705
    Thank Post
    3,239
    Thanked 1,045 Times in 967 Posts
    Rep Power
    363
    2. Using variables in Python | Pi Programming

    Code:
    
    Taking Input
    Lets have a look how we can read in a value from the user and print that out. Take the following code:
    
     import sys
     message = "You typed in: "
     print("Please type something: ")
    input_line = sys.stdin.readline()
    
     print(message + input_line)
    
     Now, we have a couple of new parts here. The first line has an import  which imports sys. Sys is a library which contains more useful code  that is commonly used by people and comes as standard with Python.  Specifically we are using it on the line that reads ‘input_line =  sys.stdin.readline()’. 
     The sys.stdin.readline() part reads in what the user types until the  point they press return. It then assigns that value to the variable  ‘input_line’.
     We then come to the final print message. This uses two variables and  prints them together. The + symbol means that python will add the two  string together, i.e. the message and the input_line. Run the code and  you will see the output.
    Last edited by mac_shinobi; 12th May 2014 at 04:18 PM.

  7. #7
    Bev
    Bev is offline

    Join Date
    Oct 2009
    Posts
    157
    Thank Post
    13
    Thanked 5 Times in 5 Posts
    Rep Power
    11
    Quote Originally Posted by bladedanny View Post
    Do you have python 2.7.6 installed as well?
    I think I must do, I thought I'd deleted 2.7 but it doesn't seem to have gone.

  8. #8
    Bev
    Bev is offline

    Join Date
    Oct 2009
    Posts
    157
    Thank Post
    13
    Thanked 5 Times in 5 Posts
    Rep Power
    11
    import sys
    message = "You typed in: "
    print("Please type something: ")
    input_line = sys.stdin.readline()

    print(message + input_line)
    That works, running either way.

    And that looks a useful link - thank you.

  9. Thanks to Bev from:

    mac_shinobi (12th May 2014)

  10. #9

    Join Date
    Nov 2010
    Posts
    433
    Thank Post
    58
    Thanked 66 Times in 57 Posts
    Rep Power
    27
    Code:
    name = raw_input("What is your name? ")
    print "Hi", name
    Excellent book link.

  11. Thanks to mats from:

    mac_shinobi (16th May 2014)

  12. #10
    duxbuz's Avatar
    Join Date
    Jan 2010
    Posts
    338
    Thank Post
    14
    Thanked 1 Time in 1 Post
    Blog Entries
    1
    Rep Power
    0
    For me the example works in 3.3 shell, but I get that error in the 2.7 shell.

    Must be running 2.7 as default shell.

    I think I had to run python in the shell using the line python3 on raspberrypi Linux if I remember, wonder if its same on mac?

    If I just invoked it with python I would end up in 2.7 shell
    Last edited by duxbuz; 16th May 2014 at 09:03 AM.

SHARE:
+ Post New Thread

Similar Threads

  1. Basic Help Needed!
    By CHiLL in forum Home Access Plus+
    Replies: 134
    Last Post: 12th December 2011, 09:35 AM
  2. Replies: 8
    Last Post: 22nd February 2008, 12:34 PM
  3. Help! Need a BIOS re-flashed
    By ajbritton in forum Hardware
    Replies: 2
    Last Post: 28th December 2006, 12:16 PM
  4. Help needed to reconfig my DC's
    By tosca925 in forum Windows
    Replies: 16
    Last Post: 20th November 2006, 10:45 PM
  5. Help needed creating a DMZ
    By pooley in forum Wireless Networks
    Replies: 12
    Last Post: 11th January 2006, 10:42 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •