LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2011, 10:24 AM   #1
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 203
Blog Entries: 3

Rep: Reputation: 24
Python language question: beginner


I have these two simple python scripts however they do not run due to the following errors:

./faren.py
./faren.py: line 7: Note - you should be careful of the order in
which you write your conditions so that you don't
end up masking one condition behind another (this
comment applies to all languages - not just Python): command not found
./faren.py: line 9: syntax error near unexpected token `('
./faren.py: line 9: `faren = input("Enter a temperature") * 9.0 / 5 + 32'
----------------------------------------------------------------------------
faren = input("Enter a temperature") * 9.0 / 5 + 32
print "That degrees converts to",faren
if faren > 212:
print "Steam"
elif faren > 112:
print "Very Hot Water"
elif faren > 32:
print "Water"
else:
print "Ice"
print "Program completed"
-----------------------------------------------------------------------------

./age.py
File "./age.py", line 17
print "A negative age is not possible."
^
IndentationError: expected an indented block
-----------------------------------------------------------------------------
age = input("Enter your age (in human years): ")

print # print a blank line

# check if the age is valid using a simple if statement

if age < 0:

print "A negative age is not possible."

elif: age < 3 or age > 110:

print "Frankly, I don't believe you."

else:

print "That's the same as a", age/7, "year old dog."
-----------------------------------------------------------------------------

Can anyone point out what needs to be edited, (maybe add the backslash character "\"?).
 
Old 05-23-2011, 10:30 AM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
In python code-indentation is essential. If you don't put your scripts into code-tags no one here can see the indentation and it is impossible to help you with your problems, especially if you get IndentationErrors.
 
Old 05-23-2011, 11:40 AM   #3
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 203

Original Poster
Blog Entries: 3

Rep: Reputation: 24
Quote:
Originally Posted by TobiSGD View Post
In python code-indentation is essential. If you don't put your scripts into code-tags no one here can see the indentation and it is impossible to help you with your problems, especially if you get IndentationErrors.
Using the python editor 'Kate' and the script is as follows:

# Conditional example 4 - if / elif / else

""" Note - you should be careful of the order in
which you write your conditions so that you don't
end up masking one condition behind another (this
comment applies to all languages - not just Python)"""

faren = input("Enter a temperature"\) * 9.0 / 5 + 32
print "That degrees converts to",faren
if faren > 212:
print "Steam"
elif faren > 112:
print "Very Hot Water"
elif faren > 32:
print "Water"
else:
print "Ice"
print "Program completed"

""" Sample results:

-bash-3.2$ python if4.py
Enter a temperature 33
That degrees converts to 91.4
Water
Program completed
-bash-3.2$ python if4.py
Enter a temperature -5
That degrees converts to 23.0
Ice
Program completed
-bash-3.2$

Next script:

#!/usr/bin/env python

# All comments start with a the character "#"

# This program converts human years to dog years

# get the original age

age = input("Enter your age (in human years): ")

print # print a blank line

# check if the age is valid using a simple if statement

if age < 0:

print "A negative age is not possible."

elif: age < 3 or age > 110:

print "Frankly, I don't believe you."

else:

print "That's the same as a", age/7, "year old dog."

The indentations refer to which part of the scripts?/ What should be added to them?
 
Old 05-23-2011, 11:43 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
What about the code tags?
 
Old 05-23-2011, 11:55 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
For the OP, you need to put your code between "[code]" and "[ /code]", so that the indentation is preserved. Note that there shouldn't be a space in the closing tag.
 
Old 05-23-2011, 12:10 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Nylex View Post
Note that there shouldn't be a space in the closing tag.
You can use [noparse] tags to stop other tags from being interpreted.
 
1 members found this post helpful.
Old 05-24-2011, 04:56 AM   #7
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 203

Original Poster
Blog Entries: 3

Rep: Reputation: 24
age = input("Enter your age (in human years): ")

print # print a blank line

# check if the age is valid using a simple if statement

<indent>

<% if age < 0: %>

<% print "A negative age is not possible." %>

<% elif: age < 3 or age > 110: %>

<% print "Frankly, I don't believe you." %>

<% else: %>

<% print "That's the same as a", age/7, "year old dog." %>

</indent>

Firstly did I use the correct indentation tags?

Secondly should the tags be at the very start of the code or as in the above example, surrounding the if/else/elif statements?
 
Old 05-24-2011, 05:51 AM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
You have to put the whole code into the code tags, so that it looks for example this way:
Code:
[CODE]
unindented code
    indented code
    indented code
          more indented code
unindented code
[/CODE]
This will make it look like this:
Code:
unindented code
    indented code
    indented code
          more indented code
unindented code
 
Old 05-24-2011, 06:13 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by shayno90 View Post
Firstly did I use the correct indentation tags?
Nylex said this in a previous post:

Quote:
put your code between "[code]" and "[ /code]", so that the indentation is preserved. Note that there shouldn't be a space in the closing tag.
 
Old 05-24-2011, 08:18 AM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
The first program listing is taken from here:

http://www.wellho.net/resources/ex.p...em=y103/if4.py

The second program listing is part of the "Linux Newbie Administration Guide," and it's been reproduced here:

http://linux.about.com/library/bl/op...e7.2python.htm

Quote:
IndentationError: expected an indented block
People, he didn't just forget to use CODE tags to format his post; he actually didn't have indentation in his Python source file.

shayno90, you retyped the programs and removed all the indentation. That's why it didn't work. Put the indentation back in.

Last edited by dugan; 05-24-2011 at 08:41 AM.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What language is the easiest for a beginner? worm5252 Programming 19 12-12-2009 08:08 PM
LXer: Review: Programming in Python 3: A Complete Introduction to the Python Language LXer Syndicated Linux News 0 01-26-2009 04:50 AM
Beginner Python Question Chronothread Programming 5 08-05-2008 11:12 PM
Best Beginner programing language... computer_tom Programming 95 12-20-2007 02:58 PM
graph problems - python is the language but math is the question cs-cam Programming 3 09-03-2005 10:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration