if-else-then

T

TuPLaD

Hi, i got the following script:

name = raw_input("Please enter your name: ")
print "Your name is", name

but i want it that way:

if
name = Stickie
then
print "You be di man"

how do i do it ?

This i what i have from a tutorial, but with syntax errors i dont know
what im doing wrong :(

#!C:\Python23\python.exe
# My first Python Application !
# By TuPLaD
# (e-mail address removed)

import time
def main(n):
name = raw_input("Please enter your name: ")
if name = Stickie:
print 'Yo masta', name
else:
print "Your name is", name

time.sleep(5)

what should i do ?
 
P

Phil Frost

"=" and "==" are two different operators in Python. "=" is the asignment
operator; it asigns values to names. "==" is the equality operator; it
returns true if its operands are equal.

Also, to compare name to a string, the string must be in quotes.
Without quotes, python will look for a varable named "Stickie".

Furthermore, defining main won't make it run. A common idiom is to check
__name__ and run main. If the file is being run as a program, as opposed
to being imported as a module, __name__ will be "__main__".

Also, I don't see need for the "n" parameter, or the time.sleep(). I
have removed them.

Here is your program with these points applied:

#===========

def main():
name = raw_input("Please enter your name: ")
if name == "Stickie":
print 'Yo masta', name
else:
print "Your name is", name

if __name__ == '__main__':
main()

#===========
 
T

TuPLaD

Thank you very much !

But i got one more question, i was googling in python sites, and i
found a site where he he a code like this :


if user type: n, no, nop, nope then print 'You canceled'
else
if user type: y, ye, yes, yep then print 'We are beginning'


How do i do that ?
 
F

Francesco Bochicchio

Thank you very much !

But i got one more question, i was googling in python sites, and i
found a site where he he a code like this :


if user type: n, no, nop, nope then print 'You canceled'
else
if user type: y, ye, yes, yep then print 'We are beginning'


How do i do that ?
Not much differently from what you have already written :)

user_string= raw_input("Tell me:")
if user_string in 'y', 'ye', 'yes', 'yep':
print "We are beginning"
elif user_string in 'n', 'no', 'nop', 'nope':
print "You canceled"
 
T

Thorsten Kampe

* TuPLaD (2004-09-11 14:34 +0200)

Please don't fullquote.
if user type: n, no, nop, nope then print 'You canceled'
else
if user type: y, ye, yes, yep then print 'We are beginning'

How do i do that ?

There's a tutor list where people who are constanty bored just wait
for questions like this.

Excerpt from a template.py of mine:

def equivalent_answers(answer):
""" generate equally valid answers ('y', 'ye', 'yes') """
return [answer[:index + 1] for index in range(len(answer))]

MyOptions = {'verbose': None}

myanswer = raw_input('%s%s%s' % ('verbose',
'? [yes|no|ignore|abort]: '),
[n])).lower()

if myanswer == '': # 'empty' answer
MyOptions['verbose'] = False

elif myanswer in equivalent_answers('yes'):
MyOptions['verbose'] = True

elif myanswer in equivalent_answers('no'):
MyOptions['verbose'] = False

elif myanswer in equivalent_answers('ignore'):
pass

elif myanswer in equivalent_answers('abort'):
pass

else:
print "ignoring invalid answer: '%s'" % myanswer
 
T

Thorsten Kampe

* Thorsten Kampe (2004-09-11 15:56 +0200)
* TuPLaD (2004-09-11 14:34 +0200)

There's a tutor list where people who are constanty bored just wait
for questions like this.

Excerpt from a template.py of mine:

In fact my script is a bit stricter than your question: it doesn't
allow 'yep' or 'nononono'. Some utilities allow that (probably with a
reason) and even non localised answers (for instance in German 'j',
'ja' (pronounced 'yaa') would be valid answers, but also 'y', 'ye' and
'yes' are allowed).

If you want that you would only have to ask whether the answer starts
with the same letter as your valid answer.

Thorsten
 
T

TuPLaD

Thorsten Kampe said:
In fact my script is a bit stricter than your question: it doesn't
allow 'yep' or 'nononono'. Some utilities allow that (probably with a
reason) and even non localised answers (for instance in German 'j',
'ja' (pronounced 'yaa') would be valid answers, but also 'y', 'ye' and
'yes' are allowed).

If you want that you would only have to ask whether the answer starts
with the same letter as your valid answer.

Thorsten

I dont need anything like that now :) I just started learning the language ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top