[Tutor] problem with code
alan.gauld at btinternet.com
Thu Aug 20 19:07:55 CEST 2015
On 20/08/15 10:50, Nathan Clark wrote:
> I have written a basic program out of python and it is not functioning,
> please could you proof read my code and tell me how to fix it.It is in
> python 3.3
> time=int(input("How long on average do you spend on the computer per day?")
> (print("that seems reasonable")) if time<=2
> else print ("get a life")
When you are programming you have to follow the rules of the
language very carefully. Its not like English, say, where
you have a lot of flexibility in the order that you say
things. Also you have to get the punctuation (the syntax)
exactly right, any missing commas, or brackets, or quotes
or, in your case, colons will stop your code working.
Taking your last two lines, you need to spell them like this:
print("that seems reasonable")
print ("get a life")
The if must come first.
There must be a colon after the if expression
and the thing you want executed.
There must also be a colon after the else and the thing
you want executed.
There is an alternative way of writing what you want but
it's not very commonly used:
print("that seems reasonable" if time <=2 else "get a life")
Notice that the if/else are all inside the print's parentheses.
The expression basically passes a single string to print depending
on the test result. You could expand it like this instead:
message = "that seems reasonable" if time <=2 else "get a life"
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:
More information about the Tutor