473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

if-else-then

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
# sp*****@pandora.be

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 ?
Jul 18 '05 #1
6 1718
"=" 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()

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

On Fri, Sep 10, 2004 at 04:34:58PM -0700, TuPLaD wrote:
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
# sp*****@pandora.be

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 ?

Jul 18 '05 #2
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 ?

Phil Frost <in****@bitglue.com> wrote in message news:<ma**************************************@pyt hon.org>...
"=" 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()

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

On Fri, Sep 10, 2004 at 04:34:58PM -0700, TuPLaD wrote:
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
# sp*****@pandora.be

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 ?

Jul 18 '05 #3
On Sat, 11 Sep 2004 05:34:42 -0700, TuPLaD wrote:
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"
Jul 18 '05 #4
* 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
Jul 18 '05 #5
* Thorsten Kampe (2004-09-11 15:56 +0200)
* TuPLaD (2004-09-11 14:34 +0200)
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:


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
Jul 18 '05 #6
Thorsten Kampe <th******@thorstenkampe.de> wrote in message news:<1m***************@thorstenkampe.de>...
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 ;-)
--------------------------------------------------------------------------------

Thx for the answers everyone !
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
5
by: Steve | last post by:
Hello, I've been a PHP programmer for a number of years and have just started to learn JS. My Employer (a water analysis lab) wants what should be a very simple .js written that basically takes...
14
by: Santi | last post by:
I see in some code, I donīt remember now if it is c# or c++, that the when they perform a comparison they use the value first and then the variable, like: if( null == variable ){} Is there an...
0
by: Benny Ng | last post by:
Hi,All, When i deploy Enterprise library with my application ,i used XCOPY to deploy it into my test server. But when application runs, shown some error related registry. (But actually I haven't...
40
by: Jeff | last post by:
I have a system on a network and want to determine if anyone is currently connected to the back-end files. An interesting twist is that I have noticed that some users can be connected (have the...
2
by: gdarian216 | last post by:
the object of the project was to create a program that inputs multiple lines of integers and outputs them in an HTML table. The negative numbers should be red. The numbers should be printed 3 in a...
6
by: =?Utf-8?B?UmljaA==?= | last post by:
In C# you have If ... { { ... } } If you place the mouse cursor next to a bracket and press ctrl something
2
by: .rhavin grobert | last post by:
hello;-) i have that following little template that defines some type of vector. it works with structs and i want to use it also for simple pointers. the problem is, in following function......
1
by: grego9 | last post by:
I have a problem in Excel 2000. I am trying to return a value in Column F based on what is Column E. There are about 14 different text variants in Column E (all look like "2008H1", "2008H2)). ...
6
by: joe2k1 | last post by:
Hello everyone, I have a question about if statements with xml. I need to ensure certain fields have a particular value, if this is true I want to display a large amount of text. Let me give an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.