petethefeet
Posts: 23
Joined: Wed Nov 21, 2012 10:01 am

Reading keyboard input

Wed Feb 20, 2013 10:57 am

Hey,

I'm learning Python and wanting to write a program the when I give a physical input I get an output.

For a start I'd like to press a key on the keyboard and get a Print output, but I can't seen to get off the ground.

Anyone give me the tools to get started?

Thanks

Pete
New but willing to learn

Pete

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Reading keyboard input

Wed Feb 20, 2013 11:19 am

For Python 2:

Code: Select all

name = raw_input("What is your name? ")
print "Hello", name
In Python 3:

Code: Select all

name = input("What is your name? ")
print ("Hello", name)
raw_input returns a string. You can convert that to an integer for example like this:

Code: Select all

text = input("Pick a number: ")
number = int(text)
print ("Half of that is", number/2)
although to be entirely correct, it would be a good idea to catch the exception that will happen when the user types something that is not a number. This is left an an exercise for the interested reader ;-)

petethefeet
Posts: 23
Joined: Wed Nov 21, 2012 10:01 am

Re: Reading keyboard input

Fri Feb 22, 2013 3:50 am

Cheers for that, got me going nicly.

Pete
New but willing to learn

Pete

thivtulz
Posts: 56
Joined: Tue Dec 18, 2012 10:05 am

Re: Reading keyboard input

Tue Mar 19, 2013 6:24 am

How to send data from PC to RPi using python code through the UART?Please help me on this.

thivtulz
Posts: 56
Joined: Tue Dec 18, 2012 10:05 am

Re: Reading keyboard input

Tue Mar 19, 2013 9:15 am

please solve this problem to me..How to send data from PC to RPi using python code through the UART?Please help me on this.. :( :( :( :(

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Reading keyboard input

Tue Mar 19, 2013 10:00 am

Your query is nothing to do with the subject of this rather old thread. You have two perfectly good threads on this subject already, one of which I have answered. Locking this one.

Return to “Python”