Follow us on Twitter!
It is the path of least resistance that makes rivers and men crooked. - Bj Palmer
Monday, April 21, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 29
Guests Online: 26
Members Online: 3

Registered Members: 82858
Newest Member: alexxkim
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Python pulling info


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 21-02-10 05:49
Hello everyone, I am new here to HBH, and have recently started learning Python. It is really relatively simple to learn, maybe because I already have some background in PHP, HTML, CSS, and JavaScript, but I was wondering if someone could help me out here.
My question is how do I pull out out a certain string, input(ed) by a user and make it come out as something else. This is not very clear, maybe if you see some of my code it will make more sense
Code

a = 1 #obviously the letter 'a' is the variable which is equivalent to 1
use = raw_input("Please enter the letter 'a': "); #user inputs a letter in this case 'a'
if use == 'a':
   print a;





Okay, as most of you who do any type of Python, or perhaps JAVA and C++ programming may know this code will allow a user to input a string as long as it is 'a', it will output 1. Now, what I would like to be able to do is have it equal grab the letter 'a' out of the text the user submits and automatically print a. I thought about doing this with a Function, but I am not quite exactly sure how to go about this. If this is still unclear, please let me know and I will try harder to make it a bit more comprehensible.
Thanks
Author

RE: Python pulling info

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 21-02-10 07:25
First off, Welcome to HBH :)

Secondly, I'm not sure what *exactly* your trying to accomplish, however I came up with a few ideas of what you might be trying to do.

If you are trying to get it to print the 1st letter of whatever is inputted by the user (a, b, c, no matter what it is) you can use indices.
Code
variable = raw_input("Enter the letter 'a': ")
print variable[0]




If the user enters 'a', that will print 'a'. If the user enters 'apples', that will print 'a'. If the user enters 'carrots', that will print 'c'.

Another thing I thought you might be trying to do is find 'a' anywhere in the string and if it's there, print 1. This can be done though the string's find function
Code
a = 1
variable = raw_input("Enter the letter 'a': ")
if variable.find('a') != -1:
   print a




If the user enters 'a', it will print 1. If the user enters 'carrots', it will print 1. If the user enters 'bog', it won't print anything. If that's what you were looking for I can further evaluate how that works.

Hope that helps :)
If not, please explain a little better.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .

Edited by stealth- on 21-02-10 07:26
http://www.stealth-x.com
Author

RE: Python pulling info


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 21-02-10 19:47
Thanks for the welcome and a lot for the code. I think the second line(s) of code is more of what I am looking for thank you =) I am just learning Python lol it's only been a few days, but I think I'm starting to get a grasp on it lol.
Thanks again for the code
Author

RE: Python pulling info

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 21-02-10 20:58
Anytime :)

In case you were wondering, find() is a function of a string. Therefore, this will work on every string you can come up with, but only strings. You give find() a string that you want it to look for, in our case 'a', and it returns a integer which is the location of the first occurrence of that string. If it does not find the string, it will return -1.

So find() can be used for other things, such as telling where *exactly* they entered 'a'. Like this:
Code
variable = raw_input("Enter a phrase containing 'a': ")
location = variable.find("a")
if location == -1:
   print "That phrase doesn't contain a 'a'!"
else:
   print "The first occurrence of 'a' was at:"
   print location




Keep in mind though that python starts counting at 0. So if you entered "cat", it would return a 1, not a 2, like you would expect.

Feel free to PM me if you need any more help :)


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .

Edited by stealth- on 21-02-10 21:00
http://www.stealth-x.com
Author

RE: Python pulling info

michelfalke
Member

Your avatar

Posts: 9
Location: Longitude l = new Longitude();
Joined: 08.01.10
Rank:
Guest
Posted on 21-02-10 22:28
This is a link i personally used to kick off in python it thought me the basics of how python works Smile

http://www.dickba. . .ocpyth.htm

Don't know if it's any use for u.


School sux hbh teaches me way more =)
michelfalke50@hotmail.com
Author

RE: Python pulling info


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 21-02-10 23:06
Thanks again, to the both of you =) I will be sure to check out that link ! and I will most likely take you up on the PM offer stealth- =P I'll try not to annoy you with a ton of questions LOL
Author

RE: Python pulling info

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 21-02-10 23:11
Cr4zyM4n123 wrote:
Thanks again, to the both of you =) I will be sure to check out that link ! and I will most likely take you up on the PM offer stealth- =P I'll try not to annoy you with a ton of questions LOL


Heh, send as many as you'd like. I enjoy helping people with python stuff Smile


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .
http://www.stealth-x.com
Author

RE: Python pulling info

techb
Member



Posts: 384
Location:
Joined: 15.02.09
Rank:
Moderate
Posted on 23-02-10 07:02
it looks like i'm posting in this thread a little late, but i figured i'd throw in my two cents.

instead of find() you could also use index()

v = raw_input("Enter something with 'a': "Wink
try:
print "'a' found at %d"
except: print "'a' not found"


i used index() ALLOT when i was reading pixels from my webcam.

oh and i'm also new to hbh, what tags do i use to put code in?

Edited by techb on 23-02-10 07:03
kbcarte.wordpress.com
Author

RE: Python pulling info

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 23-02-10 15:59
techb wrote:
it looks like i'm posting in this thread a little late, but i figured i'd throw in my two cents.

instead of find() you could also use index()

v = raw_input("Enter something with 'a': "Wink
try:
print "'a' found at %d"
except: print "'a' not found"


i used index() ALLOT when i was reading pixels from my webcam.

oh and i'm also new to hbh, what tags do i use to put code in?


Nice, I'd forgotten about that Smile

You can use the [co'de] and [/co'de] tags. Just ignore the ' I put in each tag to avoid it actually showing up in code tags. There are links right below the response window for common things like quotes, imgs, centered, and so on.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .
http://www.stealth-x.com