Jump to content

Python - If Statements


Recommended Posts

I'm so not used to this. What I'm usually doing with VB .NET is telling it where the variable is coming from and what it's going to be to determine the outcome of an If Statement, but I'm not too sure with Python as I started only earlier today.

I'm trying to build a quick program that asks the user for your name, welcomes you, and then asks how you are with three possible answers - happy, sad, neutral.

I want to test the raw_input to determine what's going to be the next print message, and if the user decides to type something else it will fail as another message says please enter one of the three answers - happy, sad, neutral.

This is the code...

print "Hello, what's your name partner? "

n = str(raw_input())

print "Hello, " + n + "! Tell me how you're feeling. Are you happy, sad, or neutral?"

and this is an attempt at an If Statement I can use with it which doesn't work.

If x = Happy:
print "That's good!"
elif x = sad
print "Aww. What's wrong?"
elif x = neutral
print "Welcome to my world partner!"

Did I do it right nearly? It doesn't work so I might have missed something out. If so, would I need a For Loop?

Link to comment
Share on other sites

I'm just using them as a string at the minute. It is supposed to mean that, that is what the human types on the keyboard.

Maybe I should explain what I'm doing a bit better.

Computer says "Hello. What's your name?"

Human says " *name*

Computer says "How are you, and gives 3 choices"

human says " one of three choices"

computer says " something to go with the choice"

computer says "so how old are you"

human says " *age* "

computer says "crikey that's old "

or something along those lines.

This has got nothing to do with the tutorial I'm using at the minute. I just thought I should practice what I've done so far before going further.

Link to comment
Share on other sites

A string is a variable consisting of a number of characters, containing data such as 'h', 'a', 'p', 'p', 'y'.

A constant or literal value contains a fixed value such as "happy" (or of course a numeric value such as 723).

You should not use variables if constants will do.

Link to comment
Share on other sites

There are lots of aspects to programming; one of the most important is program readability. This is something you should learn early on. If you ever write a professional application, chances are that someone else will have to look into your program - either updating it, or supporting it.

A good readable program is much easier to maintain or support than a "clever" one. Using constants / literals where possible adds a lot to the readability of a program.

Link to comment
Share on other sites

  • 3 weeks later...
I'm just using them as a string at the minute. It is supposed to mean that, that is what the human types on the keyboard.

Maybe I should explain what I'm doing a bit better.

Computer says "Hello. What's your name?"

Human says " *name*

Computer says "How are you, and gives 3 choices"

human says " one of three choices"

computer says " something to go with the choice"

computer says "so how old are you"

human says " *age* "

computer says "crikey that's old "

or something along those lines.

This has got nothing to do with the tutorial I'm using at the minute. I just thought I should practice what I've done so far before going further.

name = raw_input("Hello. What's your name? ")
mood = raw_input("How are you feeling?\n1) Happy\n2) Sad\n3) Neutral\n: ")
if mood in ("1", "2", "3"):
if mood == "1":
print "That's Great!"
elif mood == "2":
print "That's Too Bad"
elif mood == "3":
print "Interesting"
else:
print "Invalid response"
age = raw_input("How old are you? ")
try:
print "%s! That's old!" % int(age)
except:
print "%s is not an age." % age

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy