This Forum has been archived there is no more new posts or threads ... use this link to report any abusive content
==> Report abusive content in this page <==
Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with Python 2.6 if/then/else statements.?
10-03-2012, 01:15 AM
Post: #1
Trouble with Python 2.6 if/then/else statements.?
I am entered in a programming course at school. Trouble is, I suck at programming!
I am struggling greatly with a question. The idea of it is for the the program to prompt the user how many characters he/she wrote. Once it has that input, if the amount is less than 140 it is to print "Thats for twitter" if it is more that 141 and less than 5000 it is to print "Thats for facebook" and for anything more than 5000 it is to say "Thats for you blog."
Here is my code:
characters=input("How many characters did you write?")
if characters >= 5000:
print "That belongs on your blog."
if characters <= 4999:
print "That belongs on facebook."
if characters <= 140:
print "Thats for twitter"

The problem I am having is if I put in a number lower than 140 it says "That belongs on facebook" and underneath it says "Thats for twitter". How to I specify for it to be less than 4999 BUT more than 140, then say "That belongs on facebook"

I hope this makes sense to you guys, haha.

Thanks very much for your help, its very much appreciated Smile

Ads

Find all posts by this user
Quote this message in a reply
10-03-2012, 01:23 AM
Post: #2
 
Good question. I'll post some fixed code below, but first I want you to see the error in your logic. Notice that if a message has 120 characters, it has less than 141 characters. But the problem is that it also has less than 4999 characters. Because it meets that condition first, it will print "That belongs on facebook."

You've probably heard it said in your programming course that "computers only do what you tell them to do," meaning computers can only execute exact instructions. No more, no less. So, you need to create some exact instructions.

You said it in plain English pretty well. I mean I understood it. So let's use that as your starting point. I'll use the phrases you wrote and turn them into Python logic statements below. Each line of Python code will begin with ">>>"

if the amount is less than 140 it is to print "Thats for twitter"
>>> if characters <= 140:
>>>     print "Thats for twitter"

if it is more that 141 and less than 5000 it is to print "Thats for facebook"
>>> if 141 <= characters <= 5000:
>>>     print "That belongs on facebook."

for anything more than 5000 it is to say "Thats for you blog."
>>> if characters > 5000:
>>>     print "That belongs on your blog."

These are all exact statements that don't encroach on each other's territory. Maybe you didn't know you could write multiple conditions in an if statement (141 <= characters <= 5000) and that would have been helpful to know. But now we'll put it all together and try it out:


characters = input("How many characters did you write? ")
if characters <= 140:
    print "Thats for twitter"
elif 141 <= characters <= 5000:
    print "That belongs on facebook."
elif characters > 5000:
    print "That belongs on your blog."


This could be written differently. It could definitely be written without the "else if" parts, or with a single "else" statement for the last condition without the condition written out, but I did it anyway just to be explicit. There are also some "best practices" I ignored for simplicity's sake (using input() instead of raw_input(), etc.). But this code does what you want it to do.

Remember, you basically wrote this code, you just did it in plain English first and I converted it into formal logic, almost word for word. Give it a try, and the next time you run into a problem like this, write it out in your own words and then try to convert your statements into formal logic.

Let me know if you still need help.

Ads

Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)