Ask your own question, for FREE!
Conditionals & Control Flow 21 Online
OpenStudy (shedrack):

Please I am finding it hard to get the desired results of some codes I saw in the material. Ch2. The if and else statements. Once I use the else statements, it starts bringing errors.

OpenStudy (hari5719):

which part show ur code ??

rvc (rvc):

if(condition) { //statements //statements } else { //statements //statements }

rvc (rvc):

basic syntax

OpenStudy (shedrack):

This is the code >> y = 10 >>> if y%2 == 0: print y, "is even" else: print y, "is odd" File "<pyshell#74>", line 3 else: ^ IndentationError: unindent does not match any outer indentation level

OpenStudy (shedrack):

Thanks. I have seen the error. It was due to a poor indentation.

OpenStudy (shedrack):

def printParity(z): if z%2 == 0: print z, "is even" else: SyntaxError: invalid syntax Once I type else:, it gives me the syntax error above. This is the remainder of the code. else: print z, "is odd". Then I need to call the function and check parity of any number

OpenStudy (shedrack):

The basic problem is how to define my functions, calling it and printing desired result. Thanks

OpenStudy (rsmith6559):

You've gone from doing this in the shell to doing this in a file of some sort. Editors can introduce errors. I gave up using IDLE because it changed my <tab> to <space> which as far as visually is concerned is no big deal, but it angers Python. Python regards, say, a tab character as THE indentation for a block of code. Try putting in four spaces instead and it looks the same, but Python will refuse to run it. What you're doing is (depending on invisible characters) right. I could call your function like: printParity( myVariable ) and it would run. Occasionally, you'll see a function like yours written: ``` def printParity( z ): if( z % 2 == 0 ): print z, "is even" else: print z, "is odd" ``` which works, it's just uncommon. C is probably the most influential language in history, and it's the language that the Python interpreter is written in. C defines: false == 0 true != 0 What this means is that you'll see: ``` def printParity( z ): if( z % 2 ): print z, "is odd" else: print z, "is even" ``` Most languages, Python included, are happy with that. If you use three back ticks (lowercase tilde, "~") before and after, you can post raw code on OpenStudy and it will keep the code accurate. Welcome to programming!

OpenStudy (shedrack):

Please i need help with using For loop. 1. Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4,..., 1/10. That's one simple exam that I'm not able to solve. Thanks

OpenStudy (rsmith6559):

In Python, a for statements iterates over a collection, like a list, tuple or dictionary. It's like: for item in items: # do something In Python, you can use the range() or xrange() functions to iterate over a range of values. Because of how they work, xrange() is more memory efficient, but it's really not important until you have a very large range, say a million or more. Both functions can be called with one, two or three arguments. If one argument is used, it's the value that ends the iterations. Two arguments, the beginning and the end value. Three, beginning, end and step value ( how much to increment/decrement each iteration ). Try these: for i in xrange( 10 ): print i # i will be 0 to 9 for i in xrange( 2, 11 ): print i # i will be two to ten for i in xrange( 0, 10, 2 ): print i # i will be 0, 2, 4, 6, 8 ten is beyond the range As for the rest of your program, remember that fractions are really just a division equation.

OpenStudy (shedrack):

Thanks dude

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!