Page 1 of 1

Python beginner help

Posted: Sat Jul 21, 2012 8:55 pm
by bobadopalis
Hi is it possible to set up an if statement to check an inputed name? And then output a string dependent on the name? I cant seem to work it out. So far i have:


X = raw_input ('what's your name')

If x ==
print ('hello' + x + 'your awesome')


I've tried a few ways including; if x == a with a defined as a random name and cant get it to work. Is it possible or am I going about it completely the wrong way? Sorry still getting to grips with the basics.

Re: Python beginner help

Posted: Sat Jul 21, 2012 11:55 pm
by timhoffman
Here is a simple example that just checks that a value was input
I think this what you where asking about.

Code: Select all

x = raw_input("whats your name ")
if x:
    print("hello " + x)
If you want to check for a specific name then I would do a comparison like

Code: Select all

if x.lower() == "some name":
     print "some custom response"
Note the use of lower so that you can match the name irrespective of the case the user inputs their name with. Also I noted in your example above you use "X" and "x" which wouldn't work. Remember python is case sensitive.

Cheers

Re: Python beginner help

Posted: Sun Jul 22, 2012 12:08 am
by bobadopalis
The 2nd one was what I was after, tried doing that and it wouldn't work, turns out I missed the : out. Thank you i would have been there for hours trying to work it out.

The X was a typo from my phone, i leart the hard way about python being case sensitive

Re: Python beginner help

Posted: Sun Jul 22, 2012 12:58 pm
by bobadopalis
One more question as I got such a fast response last time. Here's an example of what I've got so far.

x = raw_input ('print name')

if x.lower () == 'name1':
print (' custom response1')

if x.lower () == 'name2':
print (' custom response2')

if x.lower () == 'name3':
print (' custom response3')

else:
print ('custom response4')


The problem I'm having for name1 +2 is its also printing the else statement? And cant seem to work out why?

Re: Python beginner help

Posted: Sun Jul 22, 2012 2:09 pm
by timhoffman
Up front try and put your python code between code markup so we can see correct indent.
I think this is what you where trying to do.

Code: Select all

x = raw_input ('print name') 

if x.lower () == 'name1':
    print (' custom response1') 

if x.lower () == 'name2':
    print (' custom response2') 

if x.lower () == 'name3':
    print (' custom response3') 

else:
    print ('custom response4')

This code will always execute the else for any value of x != "name3", because each if is a seperate statement. You should use elif (which is pythons elseif

It should look like

Code: Select all

x = raw_input ('print name') 

if x.lower () == 'name1':
    print (' custom response1') 
elif x.lower () == 'name2':
    print (' custom response2') 
elif x.lower () == 'name3':
    print (' custom response3') 
else:
    print ('custom response4')
However I would probably not do it this way myself.

Code: Select all


messages = {
   "name1":"Custom response 1",
   "name2":"Custom response 2",
   "name3":"Custom response 3",
}

x = raw_input("enter name ")
print messages.get(x.lower(),"Custom response 4")

I am using a dictionary of key value pairs, and am looking up the value with the get method which takes a default argument, which represents a value to be returned if the key is not found. Keeps the code a lot smaller and more easily extended for custom responses. The if/elif chain is better if you want custom behaviour beyond just prints.

Cheers

Tim

Re: Python beginner help

Posted: Sun Jul 22, 2012 3:53 pm
by bobadopalis
I posted it on my phone and for some reason the indents were missed out when i posted...

the elif was what i was after originally thank you. But the second one definatly looks a lot cleaner and simpler. I haven't got as far as dictionary's yet, looks straight forward enough, do you know of any good links that touch on them in a bit more detail? thanks again

Re: Python beginner help

Posted: Sun Jul 22, 2012 4:58 pm
by KCarscadden
There are lots of python resources on web. Here are a few:

General documentation and tutorial:
Python 2.7 - http://docs.python.org/
Python 3.2 - http://docs.python.org/py3k/

A Beginner's Python Tutorial (lesson 6 is on dictionaries)
http://www.sthurlow.com/python/

Re: Python beginner help

Posted: Tue Jul 24, 2012 10:32 pm
by techpaul
General suggestion iuf learning python stick to 3.2 as this is a much better version with less gotchas

Re: Python beginner help

Posted: Fri Mar 22, 2019 10:14 am
by vijaydeveloper
Hi,
Previously the answer has been given for this questions
Here is a simple example that just checks that a value was input
I think this what you where asking about.

Code: Select all

x = raw_input("whats your name ")
if x:
    print("hello " + x)
If you want to check for a specific name then I would do a comparison like
Code: Select all

if x.lower() == "some name":
     print "some custom response"
[moderator added code tags]
Note the use of lower so that you can match the name irrespective of the case the user inputs their name with. Also I noted in your example above you use "X" and "x" which wouldn't work. Remember python is case sensitive.
You can learn from here.