In my case, it'll always print 'yams (newline) yams" because x is set to 5 before it checks through the loop. That's just a quick way to show the if structure of Python. If you wanted to do something with input, it'd be like this:
Code:
x = int(raw_input("PRESS FIVE FOR YAMS: "))
if x == 5:
print "yams"
print "yams"
print "yams"
else:
print "what"
print "no yams here"
print "done" |
You do parentheses the standard way. So the first line would be evaluated like this:
x = int(
raw_input("PRESS FIVE FOR YAMS: "))
raw_input is a function that returns a value that the user inputs (unless you tell Python to get its input from a file and not a user. I'm not good enough for that yet). So that function gets done first, since the innermost parentheses is part of the function. The argument "PRESS FIVE FOR YAMS" is the prompt.
So if you input a 5, this is what's done so far:
x = int("5")
Int is the function that returns the value that's put into it, in integer form. Obviously, that goes to:
x = 5
So from there, we do the next part:
You use double equal signs to compare values, and from what I know you have to end an if statement with a colon. All comparisons return either true or false. We know for sure that 5 == 5 is true (unless something is really, really screwed up), so it'll do everything in its block, as defined by the indentation.
Code:
print "yams"
print "yams"
print "yams" |
That'll print 3 lines of yams. Easy enough. The next line isn't indented, so it's not part of the "true" block. It's checked to see if it would continue this if block. else statements (and elif as well, but I don't have one here) don't run if a preceding block is true. Since the original if statement IS true, this gets skipped:
Code:
else:
print "what"
print "no yams here" |
And the next line isn't indented, so it's checked to see if it would continue the if statement again. It doesn't, it's a print statement:
So, it'll print the string "done". Here's how the program looks. I ran it from the command line because otherwise the CMD window would close after the "done" part, which is too fast for you to read: