Hb_Kai Posted January 18, 2009 Report Share Posted January 18, 2009 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? Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted January 19, 2009 Report Share Posted January 19, 2009 I have never looked at or used python, so I can't comment on the correctness of the syntax. But what I see isyou don't seem to obtain the value for x;"happy", "sad", and "neutral" - shouldn't these be constants/literals (in apostrophes)? Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted January 19, 2009 Author Report Share Posted January 19, 2009 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. Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted January 20, 2009 Report Share Posted January 20, 2009 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. Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted January 20, 2009 Author Report Share Posted January 20, 2009 Oh. Thanks Pat. I'll keep this in mind. :)It's strange because I'm used to VB .NET, which doesn't involve this as much. Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted January 21, 2009 Report Share Posted January 21, 2009 That's quite the same in every programming language; C, Pascal, VB, Cobol, even assembler code to a degree. Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted January 21, 2009 Author Report Share Posted January 21, 2009 Yeah but you mainly use string in VB .NET. There are things about Literal Constants but you don't have to use them as such... If that's understandable? Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted January 21, 2009 Report Share Posted January 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
Dodle Posted February 8, 2009 Report Share Posted February 8, 2009 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.