Discussion:
[Tutor] problem with code
Nathan Clark
2015-08-20 09:50:13 UTC
Permalink
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")
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Emile van Sebille
2015-08-20 14:44:10 UTC
Permalink
Post by Nathan Clark
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")
Check your parens -- they're mismatched.

Emile



_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-20 17:07:55 UTC
Permalink
Post by Nathan Clark
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:

if time<=2:
print("that seems reasonable")
else:
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"
print(message)

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-20 19:06:46 UTC
Permalink
Post by Alan Gauld
Post by Nathan Clark
(print("that seems reasonable")) if time<=2
else print ("get a life")
There is an alternative way of writing what you want but
print("that seems reasonable" if time <=2 else "get a life")
I just realized that the OP's version would actually work
in Python v3 because print is a function.

It effectively evaluates as

None if time <= 2 else None

But can I suggest that it's not a good style to get into.
Either of the options that i suggested in my last mail
would be more idiomatic in the Python community.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Aravind Jaya
2015-08-20 14:49:45 UTC
Permalink
time = input("How long on average do you spend on the computer per day?")
if time <= 2:
print "Message1"
else:
print "Message2"
Post by Nathan Clark
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")
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Timo
2015-08-21 10:39:24 UTC
Permalink
Post by Aravind Jaya
time = input("How long on average do you spend on the computer per day?")
print "Message1"
print "Message2"
This will raise 2 errors:
- time will be a string. So time <= 2 is invalid.
- print is a function.

Updated code:

time = int(input("How long on average do you spend on the computer per day?"))
if time <= 2:
print("Message1")
else:
print("Message2")


Also the question is ambiguous. There is no time unit. What is "long"?

Timo
Post by Aravind Jaya
Post by Nathan Clark
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")
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Mark Lawrence
2015-08-21 13:00:39 UTC
Permalink
Post by Aravind Jaya
time = input("How long on average do you spend on the computer per day?")
print "Message1"
print "Message2"
If you insist on top posting you could at least get your response
correct. The comparison will fail in Python 3.3 as time will be a
string, you've missed the conversion to int, and print should be a
function, not a statement.
Post by Aravind Jaya
Post by Nathan Clark
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")
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Nathan Clark
2015-09-01 13:07:51 UTC
Permalink
I have written another basic program out of python, please could you
instruct me on how to fix it

question= input("What is your question)
print("Let me think about that")
time.sleep(10) #10 seconds wait
print (question)
print ("lol")
#i would do it this way in python2
#maybe you can use as reference
time = int(raw_input('x'))
print 'y'
print 'z'
Good luck,
Job
Post by Nathan Clark
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?")
Post by Nathan Clark
(print("that seems reasonable")) if time<=2
else print ("get a life")
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-09-01 13:22:09 UTC
Permalink
Post by Nathan Clark
I have written another basic program out of python, please could you
instruct me on how to fix it
I assume you are using Python 3, is that correct?
Post by Nathan Clark
question= input("What is your question)
print("Let me think about that")
time.sleep(10) #10 seconds wait
print (question)
print ("lol")
Indentation is significant in Python, so to fix this, you need to align
your code to the left-hand margin. You also need to import the time
module. Lastly, you need to fix the SyntaxError on line 1. You have a
string with an opening quote, but no closing quote:

"What is your question

Add a closing quote (and a question mark).


import time
question= input("What is your question?")
print("Let me think about that")
time.sleep(10) #10 seconds wait
print (question)
print ("lol")
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-09-01 14:07:18 UTC
Permalink
Post by Nathan Clark
I have written another basic program out of python, please could you
instruct me on how to fix it
question= input("What is your question)
print("Let me think about that")
time.sleep(10) #10 seconds wait
print (question)
print ("lol")
Please, always include the full error message you get.
Firstly it helps us identify the problem without having
to copy your code or study it in detail.
Secondly it allows us to show you how to use these
messages to fix your own problems without having to
post a mail each time.

Steven has taken the time to analyze your code in
this case but please, in future, include the
error reports.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Loading...