Jump to content

Recommended Posts

Posted

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

name = input("What is your name? ")

File "", line 1, in

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

Posted

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

Posted

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.

Posted (edited)

2. Using variables in Python | Pi Programming

 

[url="http://www.piprogramming.org/main/?page_id=400"]
[/url][b]Taking Input[/b]
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.

Edited by mac_shinobi
Posted
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.

Posted
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.

  • Thanks 1
Posted (edited)

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

Edited by mirojr

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...