Why is this correct? Looks to me like it shouldn't work. - Page 5 - Overclock.net - An Overclocking Community

Forum Jump: 

Why is this correct? Looks to me like it shouldn't work.

Reply
 
Thread Tools
post #41 of 51 (permalink) Old 01-27-2018, 09:11 PM
Retired Staff
 
herkalurk's Avatar
 
Join Date: May 2010
Location: Portland, OR
Posts: 2,039
Rep: 69 (Unique: 58)
Quote: Originally Posted by PhotonFanatic View Post
The help is much appreciated guys! But at this point, I'm so new that explanations actually help me a lot more than looking at code. I can only read about half of the code that was listed. The explanation about grading a student's paper really helped though. For elif.

As for my following question, you can see the only reason they counted it wrong was because I put something on the wrong line.

So instead of

else: print "empty"

It was supposed to be:

else:
print "empty"

I was just wondering why it couldn't be on the same line. But I guess it really doesn't matter, I've come to the conclusion that the vast majority of learning to code is just memorizing the rules. So now onto my next question:

What is the difference between: %20d %20s and %s? I may just be me, but it seems like code academy has a bad habit of throwing a lot of things in the lessons that they don't bother to explain. And then, those things are included in the quizzes.

What language is this? Can you post the full code?



herkalurk is offline  
Sponsored Links
Advertisement
 
post #42 of 51 (permalink) Old 01-28-2018, 05:03 PM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,855
Rep: 19 (Unique: 18)
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.

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
post #43 of 51 (permalink) Old 02-01-2018, 10:20 PM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,855
Rep: 19 (Unique: 18)
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.

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
Sponsored Links
Advertisement
 
post #44 of 51 (permalink) Old 02-01-2018, 11:12 PM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,855
Rep: 19 (Unique: 18)
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."

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
post #45 of 51 (permalink) Old 02-02-2018, 07:19 AM
Criminal scum
 
Werne's Avatar
 
Join Date: Jan 2014
Location: Zadar, Croatia
Posts: 334
Rep: 65 (Unique: 44)
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.

Rustbucket
(9 items)
Space heater
(16 items)
Craptop
(13 items)
CPU
AMD Athlon 5350 @2.2GHz
Motherboard
Asus AM1I-A
GPU
Radeon R3
RAM
DDR3 Kingston HyperX Blu 1600MHz
Hard Drive
120GB Kingston SSDNow V300
Hard Drive
240GB SanDisk SSD PLUS
Power Supply
200W 16-24V PicoPSU
Power Supply
90W 18.7V HP laptop charger
Operating System
Debian 9 Stretch
CPU
AMD FX 8320
Motherboard
Gigabyte GA-970A-UD3
GPU
XFX AMD Radeon HD 7770 Core Edition
RAM
1x8GB DDR3-1600 G.Skill RipjawsX
Hard Drive
1TB Hitachi Deskstar 7200RPM
Hard Drive
120GB Kingston SSDNow V300
Optical Drive
Sony Optiarc
Power Supply
XFX Core Edition 550 Pro
Cooling
Scythe Mugen 4 PCGH
Case
Chieftec CH-05B-B
Operating System
Debian GNU/Linux 8 Jessie
Monitor
Samsung SyncMaster 943
Keyboard
Chicony KB-9810
Mouse
Trust GXT 14
Mouse
A sheet of A4 paper
Audio
On-board
CPU
Intel Pentium 4 2.4GHz
Motherboard
Composed of electronic circuitry
GPU
ATi Radeon IGP 340M
RAM
2x512MB DDR-233
Hard Drive
40GB IDE Hitachi Deathstar
Hard Drive
20GB IDE Hitachi Deathstar
Power Supply
Brick
Cooling
Inadequate
Case
Plastic
Operating System
Debian GNU/Linux
Monitor
Embedded 1024x768
Keyboard
Embedded
Audio
Playing
▲ hide details ▲
Werne is offline  
post #46 of 51 (permalink) Old 02-04-2018, 10:06 PM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,855
Rep: 19 (Unique: 18)
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.

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
post #47 of 51 (permalink) Old 02-08-2018, 09:39 AM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,855
Rep: 19 (Unique: 18)
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.

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
post #48 of 51 (permalink) Old 02-08-2018, 09:48 AM
New to Overclock.net
 
doritos93's Avatar
 
Join Date: Oct 2009
Location: Montreal
Posts: 2,307
Rep: 119 (Unique: 84)
Quote: Originally Posted by PhotonFanatic View Post
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?




Last edited by doritos93; 02-08-2018 at 09:51 AM.
doritos93 is offline  
post #49 of 51 (permalink) Old 02-08-2018, 09:53 AM
New to Overclock.net
 
doritos93's Avatar
 
Join Date: Oct 2009
Location: Montreal
Posts: 2,307
Rep: 119 (Unique: 84)
Actually @PhotonFanatic , I noticed that you typed by_three in the else block too. Why is that?



doritos93 is offline  
post #50 of 51 (permalink) Old 02-09-2018, 02:21 AM
Criminal scum
 
Werne's Avatar
 
Join Date: Jan 2014
Location: Zadar, Croatia
Posts: 334
Rep: 65 (Unique: 44)
Quote: Originally Posted by PhotonFanatic View Post
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?

Rustbucket
(9 items)
Space heater
(16 items)
Craptop
(13 items)
CPU
AMD Athlon 5350 @2.2GHz
Motherboard
Asus AM1I-A
GPU
Radeon R3
RAM
DDR3 Kingston HyperX Blu 1600MHz
Hard Drive
120GB Kingston SSDNow V300
Hard Drive
240GB SanDisk SSD PLUS
Power Supply
200W 16-24V PicoPSU
Power Supply
90W 18.7V HP laptop charger
Operating System
Debian 9 Stretch
CPU
AMD FX 8320
Motherboard
Gigabyte GA-970A-UD3
GPU
XFX AMD Radeon HD 7770 Core Edition
RAM
1x8GB DDR3-1600 G.Skill RipjawsX
Hard Drive
1TB Hitachi Deskstar 7200RPM
Hard Drive
120GB Kingston SSDNow V300
Optical Drive
Sony Optiarc
Power Supply
XFX Core Edition 550 Pro
Cooling
Scythe Mugen 4 PCGH
Case
Chieftec CH-05B-B
Operating System
Debian GNU/Linux 8 Jessie
Monitor
Samsung SyncMaster 943
Keyboard
Chicony KB-9810
Mouse
Trust GXT 14
Mouse
A sheet of A4 paper
Audio
On-board
CPU
Intel Pentium 4 2.4GHz
Motherboard
Composed of electronic circuitry
GPU
ATi Radeon IGP 340M
RAM
2x512MB DDR-233
Hard Drive
40GB IDE Hitachi Deathstar
Hard Drive
20GB IDE Hitachi Deathstar
Power Supply
Brick
Cooling
Inadequate
Case
Plastic
Operating System
Debian GNU/Linux
Monitor
Embedded 1024x768
Keyboard
Embedded
Audio
Playing
▲ hide details ▲

Last edited by Werne; 02-09-2018 at 05:49 AM.
Werne is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
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.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off