Jump to content

A Simple syntax problem in Python

Go to solution Solved by Midnight,

Neither

aeac7c09ea.png

or 

93f7969102.png

seems to work

 

FIrst one makes no sense anyway, so it should not work.

 

Second one, check line 10.

 

You're welcome.

So i'm trying to learn Python over at Codecademy and ran in to this problem

 

 

 

98f0645858.png

 

def distance_from_zero(a):
    if type(a) == int or type(a) == float:
        return abs(a)
        
        else return "Nope"
    
        
        distance_from_zero(5)
 
 
 
 
These are my instructions:
 
 
88e2b03609.png
 


I cant seem to figure out what the hell is causing the error, anny ideas?
thanks in forehand! 
 
(dont know wich version of python since im just learning in a webbrowser)

Redliquid~

Link to comment
Share on other sites

Link to post
Share on other sites

it's else: not else.

else:    return "Nope"

P.S.: the code tags are useful.

Link to comment
Share on other sites

Link to post
Share on other sites

it's else: not else.

else:    return "Nope"

P.S.: the code tags are useful.

90196ed74f.png

The problem persists :/ And i thought i defined distance_from_zero(a) in the first line :S

Code tags? Sorry if im grandma lvl annoying

Redliquid~

Link to comment
Share on other sites

Link to post
Share on other sites

Your indentation is wrong. Python syntax is all about indentation. Should be:

def distance_from_zero(a):    if type(a) == int or type(a) == float:        return abs(a);    else:         return "Nope";    distance_from_zero(5);

Want to solve problems? Check this out.

Link to comment
Share on other sites

Link to post
Share on other sites

 

Your indentation is wrong. Python syntax is all about indentation. Should be:

def distance_from_zero(a):    if type(a) == int or type(a) == float:        return abs(a);    else:         return "Nope";    distance_from_zero(5);

Thanks alot  :)

Redliquid~

Link to comment
Share on other sites

Link to post
Share on other sites

indentation ... that is why Pyhton is so awesome ... not

Once you learn it, it saves some time compared to typing curly braces. It results in arguably the same headache if you mess up though :P

[spoiler=My Current PC]AMD FX-8320 @ 4.2 Ghz | Xigmatek Dark Knight Night Hawk II | Gigabyte GA-990FXA-UD3 | 8GB Adata XPG V2 Silver 1600 Mhz RAM | Gigabyte 3X Windforce GTX 770 4GB @ 1.27 Ghz/7.25 Ghz | Rosewill Hive 550W Bronze PSU | Fractal Design Arc Midi R2 | Samsung Evo 250 GB SSD | Seagate Barracuda 1TB HDD | ASUS VS239H-P | Razer Deathadder 2013 Partlist

 

LTT Build-Off Thread: http://linustechtips.com/main/topic/35226-the-ltt-build-off-thread-no-building-required/

Link to comment
Share on other sites

Link to post
Share on other sites

I'm just gonna drop another problem i have here and hope someone can tell me what i'm doing wrong x)

1ad28f7f83.png


def hotel_cost(nights):

    return 140 * (nights);
    
    def plane_ride_cost(city):
        if city == "Charlotte":
            return 183;
        elif city == "Tampa":
            return 220;
        elif city == "Pittsburgh":
            return 222;
        elif city == "Los Angeles":
            return 475;




No idea why i'm failing but thanks to annyone who can help me out

2f772f9d59.png

Redliquid~

Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

i don't know python, but it seems pretty clear that the problem here is indentation again

you're declaring the plane_ride_cost function inside of the hotel_cost function

do you even need the hotel_cost function?

Link to comment
Share on other sites

Link to post
Share on other sites

 

-snip-

Another indentation issue. Remember, the indent level on python is as important as '{' in other languages (some find this annoying, but I like it because it enforces good habits from the start)

 

This is what your code is doing:

Declare hotel cost 

Return number (the return 140 * ....)

 

It isn't getting past that because a return statement breaks it out of the function, so your code is never actually getting to the line that says "def plane...." You should be able to just dedent that and the lines following that to get it to work

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to comment
Share on other sites

Link to post
Share on other sites

You can't declare a function in a function..  I was wrong

 

Also you don't need semicolons.. 

 

def hotel_cost(nights):    return 140 * (nights)def plane_ride_cost(city):    if city == "Charlotte":        return 183    elif city == "Tampa":        return 220    elif city == "Pittsburgh":         return 222     elif city == "Los Angeles":        return 475

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

 

You can't declare a function in a function.. 

 

Also you don't need semicolons.. 

 

def hotel_cost(nights):    return 140 * (nights)def plane_ride_cost(city):    if city == "Charlotte":        return 183    elif city == "Tampa":        return 220    elif city == "Pittsburgh":         return 222     elif city == "Los Angeles":        return 475

i saw a guy use semi colons further up so i thought they were supposed to be there for X reason, 

 

 

 

Neither

aeac7c09ea.png

or 

93f7969102.png

seems to work

Redliquid~

Link to comment
Share on other sites

Link to post
Share on other sites

 

You can't declare a function in a function..

 

Yes you can, in Python.

Link to comment
Share on other sites

Link to post
Share on other sites

Neither

aeac7c09ea.png

or 

93f7969102.png

seems to work

 

FIrst one makes no sense anyway, so it should not work.

 

Second one, check line 10.

 

You're welcome.

Link to comment
Share on other sites

Link to post
Share on other sites

FIrst one makes no sense anyway, so it should not work.

 

Second one, check line 10.

 

You're welcome.

A fucking space. HAH!

Thanks friend =)

Redliquid~

Link to comment
Share on other sites

Link to post
Share on other sites

Yes you can, in Python.

Ha what do you know.. I stand corrected.. 

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×