Syntax Error with str function?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 1187 Views - Last Post: 12 April 2013 - 09:01 PM Rate Topic: -----

#1 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Syntax Error with str function?

Posted 10 April 2013 - 08:34 PM

I am starting a small project to get me started with python and i hit a wall with running the code.

I get a syntax error and the first str function of that line is highlighted, Am i doing something wrong? All the letter variables involved have been defined.
if True:
    print str(y) + str(w) + str(z) + str(w) + str(x)


I appreciate the help!
Is This A Good Question/Topic? 0
  • +

Replies To: Syntax Error with str function?

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Syntax Error with str function?

Posted 10 April 2013 - 11:11 PM

Silliness of that string concatenation aside, your problem is probably that you are using python 3.
If this is the case, then you need parenthesis around the print statement:
print(str(y) + str(w) + str(z) + str(w) + str(x))


-Mek
Was This Post Helpful? 1
  • +
  • -

#3 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Syntax Error with str function?

Posted 11 April 2013 - 05:11 AM

Ok I know it it silly and probably messed up but as I said I'm just starting. Thanks fir the help Mek !
Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Syntax Error with str function?

Posted 11 April 2013 - 06:12 AM

Give string formatting a shot. This would be equivalent to your previous statement:
print("{2}{0}{3}{0}{1}".format(w,x,y,z))

It is very versatile. Generally concatenation of strings like that is discouraged in all but the simplest cases. It also tends to be pretty slow.

-Mek
Was This Post Helpful? 0
  • +
  • -

#5 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Syntax Error with str function?

Posted 11 April 2013 - 06:15 AM

So using the {} brackets will do the same thing as str function?
Was This Post Helpful? 0
  • +
  • -

#6 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Syntax Error with str function?

Posted 11 April 2013 - 06:27 AM

You can print any object and it will call the __str__ method for your object. The reason you were required to use str previously is because you were doing string concatenation and string concatenation only works between strings.

The format string method provides everything you will need. Take a look here:Format Examples

-Mek
Was This Post Helpful? 0
  • +
  • -

#7 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Syntax Error with str function?

Posted 11 April 2013 - 07:29 AM

What i am trying to create here is a date and time "getter". What i mean by this is that the computer will ask "Do you want to know the date?", and you type yes to get the date, or no to move on to the next option. The next option is time. For now i am working on the date part. So far it will give me the date if i type "yes", but will also still give me the date if i type "no". I guess i am not using the if statements correct.

from datetime import datetime


now = datetime.now ()
date = input("Do you want to know the date?")
w = "/"
x = now.year
y = now.month
z = now.day

if True:
    print (str(y) + str(w) + str(z) + str(w) + str(x))

if False:
    print (time)
    
time = input("Do you want to know the time?")
a = ":"
b = now.hour
c = now.minute
d = now.second


if True:
    print (str(B)/>/> + str(a) + str(c) + str(a) + str(d)) 



This post has been edited by tpaskell: 11 April 2013 - 07:30 AM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3488
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Syntax Error with str function?

Posted 11 April 2013 - 07:54 AM

Yes, your if statements are not correct or, at least, are not working as you would like them to. What book or tutorial are you reading? I trust that you are studying the fundamentals of the language, and not just trying to fumble your way through? You won't make much progress this way :whistling:

This post has been edited by andrewsw: 11 April 2013 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7757
  • View blog
  • Posts: 13,117
  • Joined: 19-March 11

Re: Syntax Error with str function?

Posted 11 April 2013 - 08:06 AM

View Posttpaskell, on 11 April 2013 - 09:29 AM, said:

. So far it will give me the date if i type "yes", but will also still give me the date if i type "no". I guess i am not using the if statements correct.


if True:
    print (str(y) + str(w) + str(z) + str(w) + str(x))

if False:
    print (time)




As andrewsw says, this is not doing what you think it does. What this does is, it evaluates the boolean primitive True, and if its value is "true" (which it always is), it does the folowing line. Then it evaluates the boolean primitive False, and if it's true (which it never is) it prints (time).

What you're trying to do is to evaluate some user input. So you need to get the input, and ask questions about it. For example, you might ask, is the user input equal to the string "Yes", or is it equal to either "Yes" or "yes", or does it start with the character "y", or does it represent the integer 1, or whatever. Define your condition and test it. So, in python 2.mumble you might do something like:

input = raw_input("Do you want to jump off the bridge?")
if input == "Yes, I'm attached to a bungee cord.":
  jump()
else:
  be_sensible()



In your example, you'd be playing with the contents of date, which is where you've stored the user input.

This post has been edited by jon.kiparsky: 11 April 2013 - 08:07 AM

Was This Post Helpful? 1
  • +
  • -

#10 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Syntax Error with str function?

Posted 11 April 2013 - 08:09 AM

Yes as andrewsw says, both if statements are wrong. if True will always run; if False will never run. Also as I just said... string formatting. This is what datetime was made for.

from datetime import datetime

now = datetime.now()
date = "{:%Y/%m/%d}".format(now)
time = "{:%H:%M:%S}".format(now)
print(date)
print(time)


-Mek
Was This Post Helpful? 2
  • +
  • -

#11 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Syntax Error with str function?

Posted 11 April 2013 - 08:58 AM

So I used the if else statement like john said and set it up to work with yes or Yes. But now when I say no or anything else it still prints time.

if input == "yes" or "Yes":
    print (code)
else: 
    print (time)



View Postandrewsw, on 11 April 2013 - 07:54 AM, said:

Yes, your if statements are not correct or, at least, are not working as you would like them to. What book or tutorial are you reading? I trust that you are studying the fundamentals of the language, and not just trying to fumble your way through? You won't make much progress this way :whistling:/>

I am learning what I know from the codecademy website.

This post has been edited by tpaskell: 11 April 2013 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7757
  • View blog
  • Posts: 13,117
  • Joined: 19-March 11

Re: Syntax Error with str function?

Posted 11 April 2013 - 09:01 AM

Probably using "input" as a variable name was not the smartest suggestion I've ever made. "input" is the name of a function, you don't want to use it as a variable name!
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3488
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Syntax Error with str function?

Posted 11 April 2013 - 09:18 AM

if whatever == "yes" Or whatever == "YES":
    # do something
    # or you could just use uppercase:

if whatever.upper() == "YES":
    pass

Was This Post Helpful? 0
  • +
  • -

#14 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Syntax Error with str function?

Posted 11 April 2013 - 09:18 AM

So where input is I will just replace it with user_response or something like that to create
if user_response == "yes" or "Yes":
    print (code)
else:
    print (time)



I will try this and see.

And I know I have to change the variable on the input string as well.
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7757
  • View blog
  • Posts: 13,117
  • Joined: 19-March 11

Re: Syntax Error with str function?

Posted 11 April 2013 - 09:31 AM

View Posttpaskell, on 11 April 2013 - 11:18 AM, said:

So where input is I will just replace it with user_response or something like that to create
if user_response == "yes" or "Yes":




Well, see andrewsw's post as well. Python, like many languages, is liberal about what's considered "true". Tn python

Quote

The following values are considered false:

  • None
  • False
  • zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  • any empty sequence, for example, '', (), [].
  • any empty mapping, for example, {}.
  • instances of user-defined classes, if the class defines a __nonzero__() or __len__() method, when that method returns the integer zero or bool value False.


cite

Everything else evaluates to True.

So in your expression, you have
if user_response == "yes" or "Yes":



Which is true if either user_response=="yes" or "Yes". "Yes" is always True, so this expression always comes out as True. (A or B is True if either or both of A and B are True)
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3