-1

Possible Duplicate:
Syntax error on print with Python 3

I know Python is supposed to be the god of interpreted languages... its simplicity, its lack of redundancy, blah blah blah. But since I'm too used to C, C++, Java, Javascript and Php, I must admit this is annoying. Here's my code:

#!/usr/bin/python3.1
def shit(texture, weight):
    if textura is "green":
        texturaTxt = "Shit is green"
    elif textura is "blue":
        texturaTxt = "Shit is blue"
    elif textura is "purple":
        texturaTxt = "Shit is purple"
    else:
        print "Incorrect color"
        break
    if weight < 20:
        pesoTxt = " and weights so few"
    elif weight >= 20 and peso <=50:
        pesoTxt = " and weights mid"
    elif weight > 50:
        pesoTxt = " and weights a damn lot"
    else:
        print "Incorrect weight"
    return texturaTxt + pesoTxt

c = input("Introduce crappy color: ")
e = input("Introduce stupid weight: ")
r = shit(c, w)
print r

I'm trying to learn Python and what I was trying to achieve was:

...
function shit(texture, weight) {
    string textureTxt = "Shit is ", pesoTxt = " and weights ";
    switch(texture) {
        case "green": textureTxt .= "green as an applee"; break;
        case "blue": textureTxt .= "blue as the sky"; break;
        case "purple": textureTxt .= "purple as Barny"; break;
        default: cout<<"Incorrect texture, try again later" <<endl; exit;
    }
    //etc
}
string color = "";
int weight = 0;
cout<<"Introduce color: ";
cin>>color;
//etc 
cout<<shit(color, weight);
...

But I give up, I can't make it work, it throws my all kind of errors. Hope there is some C++ or php or C to python converter out there.

Thanks for your help

Nikita 웃
  • 2,042
  • 20
  • 45
Erparom
  • 13
  • 2

1 Answers1

1

Python3 no longer supports print as a special form with the arguments following the print keyword (like print x in Python 2.x). Instead print is now a function and requires an argument list like in: print(x). See "Print is a function" in http://docs.python.org/release/3.0.1/whatsnew/3.0.html

In addition the break statement cannot occur outside of a loop. Other than the C switch statement, if does not support break. As there is no fall-through logic for if-statements it is not requred. In order to stop execution of the function and return to the caller use return.

Instead of the is operator, use the equality operator ==. is tests if objects are identical, this is a stricter test than equality. More details can be found here.

In addition you are getting the weight as a string. You might want to convert the string to an integer for comparison with other integer values, using the function int(weight).

Also there are a number of other small errors:

  • You are assigning the user input for the weight to e while you try to use the undefined variable name w in the function call
  • The first parameter in the function is called texture but you are using textura in the function body.
  • You are using peso instead of weight in one instance

Here is a (less offensive) rewrite with those errors eliminated:

def stuff(color, weight):

    color_txt = "Your stuff is "
    if color == "green":
        color_txt += "green as an apple"
    elif color == "blue":
        color_txt += "blue as the sky"
    elif color == "purple":
        color_txt += "purple as an eggplant"
    else:
        print("Invalid color!")
        return

    w = 0
    # converting a string to an integer might
    # throw an exception if the input is invalid
    try:
        w = int(weight)
    except ValueError:
        print("Invalid weight!")
        return

    weight_txt = ""
    if w<20:
        weight_txt = " and weighs so few"
    elif 20 <= w <= 50:
        weight_txt = " and weighs mid"
    elif w > 50:
        weight_txt = " and weighs a lot"
    else:
        print("Invalid weight!")

    return color_txt + weight_txt

c = input("Input color: ")
w = input("Input weight: ")
r = stuff(c, w)
print(r)