My blocks are not right!

A forum for general discussion of the Python programming language.

My blocks are not right!

Postby psychrocks » Mon Jun 30, 2014 6:17 pm

Hi!

I just started learning python and I realized that my blocks do not do what they are supposed to like in my book. Can someone help me? Thank you!

Code: Select all
>>> name = "Jessi"
>>> if name == 'Sara':
   print "Hello, Sara!"
   print "hiya"      <<<<<<<<<<<< in my book this line is supposed to be slightly left more...like not a complete part of the if statement.

   
>>> name = "Jessi"
>>> if name == 'Sara'
SyntaxError: invalid syntax
>>> if name == 'Sara'
SyntaxError: invalid syntax
>>> name = "Jessi"
>>> if name == 'Sara':
   print "hello, sara"
print "hiya"
SyntaxError: invalid syntax
>>> name = "Jessi"
>>> if name == 'Sara':
   print "Hi.":
      
SyntaxError: invalid syntax
Last edited by Yoriz on Mon Jun 30, 2014 6:20 pm, edited 1 time in total.
Reason: First post lock, Added code tags.
psychrocks
 
Posts: 5
Joined: Mon Jun 30, 2014 6:14 pm

Re: My blocks are not right!

Postby Yoriz » Mon Jun 30, 2014 6:21 pm

Welcome to the forum, please read the new users read this link in my signature.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1178
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: My blocks are not right!

Postby psychrocks » Mon Jun 30, 2014 6:31 pm

Code: Select all
>> if name == 'Sara'
SyntaxError: invalid syntax
>>> name = "Jessi"
>>> if name == 'Sara':
   print "Hello, Sara!"
   print "hiya"

   
>>> name = "Jessi"
>>> if name == 'Sara':
   print "hello, sara"
print "hiya"
SyntaxError: invalid syntax


sorry, I didn't know about pasting codes right. Here is my code
Last edited by psychrocks on Mon Jun 30, 2014 6:41 pm, edited 1 time in total.
psychrocks
 
Posts: 5
Joined: Mon Jun 30, 2014 6:14 pm

Re: My blocks are not right!

Postby Yoriz » Mon Jun 30, 2014 6:38 pm

Indent 4 spaces after the if statement line and use : and the end of if lines
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1178
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: My blocks are not right!

Postby psychrocks » Mon Jun 30, 2014 6:55 pm

I tried that :(

Code: Select all
>>> name = "Jessi"
>>> if name == 'Doug':
   print "Hello, Doug!"
   print "How are you?"

   


I need the second print line to still print "How are you?" I am in IDLE
psychrocks
 
Posts: 5
Joined: Mon Jun 30, 2014 6:14 pm

Re: My blocks are not right!

Postby Yoriz » Mon Jun 30, 2014 6:59 pm

The second print has to be outside of the blocks indentation to still print when the condition is not true
Code: Select all
>>> name = "Jessi"
>>> if name == 'Doug':
   print "Hello, Doug!"

   
>>> print "How are you?"
How are you?
>>>
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1178
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: My blocks are not right!

Postby metulburr » Mon Jun 30, 2014 7:07 pm

We are not reading the same book as you. We dont even know what book you are reading. Regardless, we do not know what you are thinking. You need to explicitly tell us what the output is suppose to be.

I realized that my blocks do not do what they are supposed to like in my book

I need the second print line to still print "How are you?" I am in IDLE

neither of these really tell use much information.


I think your syntax error is a result of improper use of the interpreter. After each block you need to have an empty space to signify that you mean the end of the block. AKA hit space twice when your done with the block. In this example you instantly put a dedented line after the if block causing a syntax error.
Code: Select all
>>> name = "Jessi"
>>> if name == 'Sara':
   print "hello, sara"
print "hiya"
SyntaxError: invalid syntax


it should be:
Code: Select all
>>> if name == 'Sara':
   print "hello, sara"

print "hiya"


PS: IDLE sucks. Horrible IDE. If you open a command prompt you will get better results. For example you will see a ... to indicate that your in the middle of a block like:
Code: Select all
>>> name = "Jessi"
>>> if name == 'Sara':
...     print "hello, sara"
... print "hiya"
  File "<stdin>", line 3
    print "hiya"
        ^
SyntaxError: invalid syntax
>>> if name == 'Sara':
...     print "hello, sara"
...
>>> print "hiya"
hiya
>>>
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1564
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: My blocks are not right!

Postby psychrocks » Mon Jun 30, 2014 7:08 pm

So...how do I know if I am printing the correct responses? How do I know that code if what is running and not just a random print code...does that make sense?
psychrocks
 
Posts: 5
Joined: Mon Jun 30, 2014 6:14 pm

Re: My blocks are not right!

Postby psychrocks » Mon Jun 30, 2014 7:09 pm

Oh okay thank you!!
its python in 24 hours :P the book
psychrocks
 
Posts: 5
Joined: Mon Jun 30, 2014 6:14 pm

Re: My blocks are not right!

Postby Kebap » Tue Jul 01, 2014 8:33 am

I think you are confusing the python interactive prompt with a python code file which is then run afterwards. The interactive prompt will immediately run every line you feed it, not wait for completion of the whole code. Not sure how IDLE differentiates these both.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 403
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests