Overclock.net - An Overclocking Community - Reply to Topic

 Title:
Message:

## Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
 02-16-2018 12:20 PM PhotonFanatic To answer you guys, I wasn't sure why I thought that would work. Right now I'm just scheming everything I think could possibly work, in an attempt to get the right answer. Often its wrong, but at least they give you the right answer if you want them to. Now I'd like to ask about a use case for abs(). Absolute value from zero. Why would we ever need to know that? It always just returns the same number. Maybe it doesn't have a - sign in front of the number anymore, but I think we can figure that out on our own. So you'd say abs(125) and it of course returns 125. Same for -125. You get 125. I'm sure there is something lying in wait in the future, when it comes to a use for this function. But I'd like to know what it is, and how abs() is helpful. 02-09-2018 02:21 AM Werne Quote: Originally Posted by PhotonFanatic And yet another question. Working on learning def. Here are the instructions: First, def a function called cube that takes an argument called number. Don't forget the parentheses and the colon! Make that function return the cube of that number (i.e. that number multiplied by itself and multiplied by itself once again). Define a second function called by_three that takes an argument called number. If that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False. Don't forget that if and else statements need a : at the end of that line! Here is my code that does not work. Code: ```def cube(number): return number ** 3 def by_three(number): if number / 3 >= 0 by_three: return cube(number) else: by_three return False``` Pretty sure my problem is here: if number / 3 >= 0 by_three But to my newb eyes it looks like it should work. Function cube is alright but function by_three has problems... Code: ```def by_three(number): # This line is ok if number / 3 >= 0 by_three: # Remove by_three from this line. Also, you should # compare the modulus of variable number and 3 to 0, not divide by # 3 and compare to 0 because, unless the number is negative, # all numbers are divisible by 3 and will return a float larger than 0. return cube(number) # Not indented properly else: by_three return False # Once again, remove by_three as it's a syntax error``` Here's how it should look like: Code: ```def cube(number): return number ** 3 def by_three(number): if number % 3 == 0: # number % 3 will return 0 (which will make the expression true) only if there is no remainder after dividing a number by 3 return cube(number) # Properly indented else: return False # removed by_three``` Note: The last line I moved below else purely because I'm used to writing code this way since I find it easier to read, the original placement works as well. By the way, why were you calling by_three on the if/else lines? 02-08-2018 09:53 AM doritos93 Actually @PhotonFanatic , I noticed that you typed by_three in the else block too. Why is that? 02-08-2018 09:48 AM doritos93 Quote: Originally Posted by PhotonFanatic And yet another question. Working on learning def. Here are the instructions: First, def a function called cube that takes an argument called number. Don't forget the parentheses and the colon! Make that function return the cube of that number (i.e. that number multiplied by itself and multiplied by itself once again). Define a second function called by_three that takes an argument called number. If that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False. Don't forget that if and else statements need a : at the end of that line! Here is my code that does not work. Code: ```def cube(number): return number ** 3 def by_three(number): if number / 3 >= 0 by_three: return cube(number) else: by_three return False``` Pretty sure my problem is here: if number / 3 >= 0 by_three But to my newb eyes it looks like it should work. Hey I've never written Python before, but it looks like your problem is indeed on that line as per some online syntax checkers If we look at the syntax of an if statement: if condition : indentedStatementBlockForTrueCondition else: indentedStatementBlockForFalseCondition and compare it to the line in question: if condition nameOfAFunctionDefinition (?) it doesn't seem to follow the correct syntax If you want to perform the function call when the condition is true, it seems as though the call should be moved to the following, indented line, right? 02-08-2018 09:39 AM PhotonFanatic And yet another question. Working on learning def. Here are the instructions: First, def a function called cube that takes an argument called number. Don't forget the parentheses and the colon! Make that function return the cube of that number (i.e. that number multiplied by itself and multiplied by itself once again). Define a second function called by_three that takes an argument called number. If that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False. Don't forget that if and else statements need a : at the end of that line! Here is my code that does not work. Code: ```def cube(number): return number ** 3 def by_three(number): if number / 3 >= 0 by_three: return cube(number) else: by_three return False``` Pretty sure my problem is here: if number / 3 >= 0 by_three But to my newb eyes it looks like it should work. 02-04-2018 10:06 PM PhotonFanatic I'm still new so I'm not sure what a code tag is. Is that why it made your code in the post look different than a regular post? I'll try to indent properly in the future, I see what you were talking about. There have been some posts where I was asking a question about the indentation though, so that might be why some of it looks wrong. But not that last post you were referring to. But thanks for the explanation. I'll have to go back and reread it a couple of times. But the %20d and the %020d seem a bit unnecessary. Couldn't you just use %s and it would sort of be a blanket function that would cover what the others do? It does not seem to have a character limit. But thanks for the help, I'm having to sort of bounce some of this stuff off of you guys, because code academy isn't always clear. And as I've shown previously, sometimes they'll even mark it as correct when the instructions say to do something different. And then wrong when you do what the instructions say lol. Kind of hinders the learning process when that is going on. 02-02-2018 07:19 AM Werne def is short for define, you define a function using it (kinda self-explanatory). Everything indented is a part of the function, it ends where the indentation ends. The %20d, %20s and the like are for string formatting. For example, %20s means it will print a 20 character string and, if shorter than 20 chars, it will have leading spaces. %d is used for integers and using %20d works in the same manner. However, using %020d doesn't, that will print a 20-digit number which, if shorter, will have leading zeros. %f works similarly but can also format the mantissa. Here's a quick example from Python shell so I don't have to write too much: Code: ```>>> x = 123456789.123456789 >>> print '%20.5f' % x 123456789.12346 >>> print '%020.5f' % x 00000123456789.12346 >>> print '%020.2f' % x 00000000123456789.12 >>> y = 15 >>> print '%7d' % y 15 >>> print '%07d' % y 0000015 >>> z = "abcdefghijklmnopqrstuvwxyz" >>> print '%35s' % z abcdefghijklmnopqrstuvwxyz >>> print '%035s' % z abcdefghijklmnopqrstuvwxyz``` There are more of those, check the documentation if you want to know about them. And, as an unrelated side-note, I thought printf-style formatting was removed from Python 3 cause of the whole format() thing but it still seems to be kicking around. As for your last post, this is a broken piece of code: Code: ```print 'Welcome to the Pig Latin Translator!' original = raw_input("Enter a word: ") if len(original) > 0 and original.isalpha(): print original else: print "You must enter a word to proceed."``` It's broken because the line "if len(original) > 0 and original.isalpha():" stands for "if the length of original is larger than 0 and if original consists of letters". If you move the original.isalpha() one line lower, you get "if the length of original is larger than 0 and if *missing part of code*" and that's a syntax error. The "and" in the code is a logical operator (like && in C) which compares len(original) and original.isalpha() to see if they're both true. If they are, it allows the code below to execute and if not, else runs instead. By the way, if you're copy/pasting code you need help with, please use code tags and proper indentation, it's much easier to read that way. 02-01-2018 11:12 PM PhotonFanatic And now for something completely the same: Another question to go with my previous post. Is code academy having an error with this? They say that my code for this lesson should look like this: print 'Welcome to the Pig Latin Translator!' original = raw_input("Enter a word: ") if len(original) > 0 and original.isalpha(): print original else: print "You must enter a word to proceed." But if you do that, it marks it as wrong. But if you do this, and move the original.isalpha() up one line to go right after "and" ... then it marks it as right. So which one is actually correct? The way they say to do it, or the way that they will mark the answer as correct? They tell you to do one thing, then they say its wrong. But they mark this as correct: print 'Welcome to the Pig Latin Translator!' original = raw_input("Enter a word: ") if len(original) > 0 and original.isalpha(): print original else: print "You must enter a word to proceed." 02-01-2018 10:20 PM PhotonFanatic Well it looks like I'll have to circle back around to those %s and %02d questions later. When I once again restart the codeacademy lessons. But for now, I'd like to get you guys' thoughts on what def is in python. My first thought is that it would sort of stand for 'define'. For example, my current lesson says: answer = "'Tis but a scratch!" def black_knight(): if answer == "'Tis but a scratch!": return True else: return False # Make sure this returns False def french_soldier(): if answer == "Go away, or I shall taunt you a second time!": return True else: return False # Make sure this returns False I understand what this code is doing completely. The only thing I don't really comprehend at this point, is this def thing. 01-28-2018 05:03 PM PhotonFanatic Its python. I will post up all the % code in regards to that question, that when I get back in front of my computer at the house. This thread has more than 10 replies. Click here to review the whole thread.