Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Evaluate my Code!
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Sniper4Life




PostPosted: Sun Apr 19, 2009 12:05 pm   Post subject: Evaluate my Code!

Hey I was just wondering if anyone could evaluate this code I did. I was only allowed to use if , elif, else , while loops , string methods , counters , accumulators , and basic input output. This was the question :

Write a program to be used by student council in this upcoming election to tabulate the votes after the election. Your program will list, and number, the three candidates for president then allow the user to enter all of the ballets until they enter 0 then tell them who won the election and what percentage each candidate earned.

Election:


print "Please select who you would like[/color] to vote for: "
print
print "1)Sir John A MacDonald"
print "2)Stephen Harper"
print "3)Anderson Cooper"
print



choice1 = int(0)
choice2 = int(0)
choice3 = int(0)
total = 0
while True:

 
    option = int(raw_input ("Number of Canidate(0 to quit): "))
    count1 = (option == 1)
    count2 = (option == 2)
    count3 = (option == 3)
   
 

    if 0 == option:
        print
        print  "Sir John A MacDonald won" , john , "% of the votes."
        print "Stephen Harper won" , stephen , "% of the votes."
        print "Anderson Cooper won" , anderson , "% of the votes."
        print
        print "There was a total of " , total , "votes."
        break
       
    elif count1:
        print
       
    elif count2:
        print

    elif count3:
        print

    choice1 += count1
    choice2 += count2
    choice3 += count3
    total += 1
    john = float (choice1 * 100 / total)
    stephen = float (choice2 * 100 / total)
    anderson = float (choice3 * 100 / total)
   
     
   


I didnt show who won because I thought the percentages clearly show who might have won.
Sponsor
Sponsor
Sponsor
sponsor
saltpro15




PostPosted: Sun Apr 19, 2009 12:21 pm   Post subject: RE:Evaluate my Code!

found a typo
code:

"Number of Canidate(0 to quit): "))


otherwise, pretty good!
Clayton




PostPosted: Sun Apr 19, 2009 12:38 pm   Post subject: RE:Evaluate my Code!

What happens if you want to add a fourth candidate to your election? Coding a program to dynamically accept any combination/number of possibilities is a very good habit to get in to.
Sniper4Life




PostPosted: Sun Apr 19, 2009 1:35 pm   Post subject: RE:Evaluate my Code!

program will list, and number, the three candidates for president then allow the user to enter all of the ballets

it asked specifically for 3 canidates
and saltpro
its not a typo


(raw_input ("Number of Canidate(0 to quit): "))


its suppose to be like this
the code still works does it not?
DtY




PostPosted: Tue Apr 28, 2009 10:38 pm   Post subject: Re: Evaluate my Code!

Looks pretty good, but there are a few things I think that would improve clarity:

* instead of just print to print a blank line, print "", imo, looks better, and would get the point across that you just want a blank line better
* choice1 = int(0), I am guessing you learned a lower level programming language previous? int(0) isn't really necessary, saying choice1 = 0, since 0 is an integer, it becomes an integer. (And later, if you do something to it involving a floating point number, it will become that automatically)
* if 0 == option: Logically, this makes sense and will work perfectly how you want, but it's saying "If zero is option", which doesn't quite sound as right as "if option is zero" (Again, there is nothing wrong with how you did it, it just looks nicer with the variable on the left)
* "There was a total of_" , total , "votes." Don't forget that the comma automatically adds a space (this is the only place you did that though, so it's probably a typo)

The last three lines (the ones that calculate the percentages) are the only ones that (I suspect) aren't doing what you planned. Imagine that the float() wasn't there, and you were doing that math without converting it to a floating point afterwards. Since all the variables are integers (and not floating points) you would get an integer back out. Afterwards, you pass it to float() and get it as a floating point decimal. All this really does is makes it display with the decimal and a trailing zero.
What (I assume) you actually want to do is do the math on those as if they were floating point numbers. You can either do this by making one of the numbers a floating point, or you can do a future import to get the new version of the division operator (if you are using 2.6 or later you already have this so ignore the last paragraph), in 2.5 or earlier though you need to add from __future__ import division to the top of your file (this needs to be before any code or you'll get a traceback)

Example of how this works:
Python:
Python 2.5.2 (r252:60911, Sep 30 2008, 15:41:38)
[GCC 4.3.2 20080917 (Red Hat 4.3.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> #Division with old method
>>> 5/3
1
>>> #Now import new method
>>> from __future__ import division
>>> 5/3
1.6666666666666667
>>> #Gives back a floating point
cavetroll




PostPosted: Tue Apr 28, 2009 11:03 pm   Post subject: Re: Evaluate my Code!

DtY @ Tue Apr 28, 2009 10:38 pm wrote:

* if 0 == option: Logically, this makes sense and will work perfectly how you want, but it's saying "If zero is option", which doesn't quite sound as right as "if option is zero" (Again, there is nothing wrong with how you did it, it just looks nicer with the variable on the left)


Putting the variable first can lead to more readable code, but can also lead to potential logic errors. If I intend to type:
code:
if option == 0:

but accidentally type:
code:
if option = 0:

The variable will be assigned the value of zero, and always execute the code in the if. This can lead to unwanted behavior. I know from personal experience spending hours trying to find the flaw in my code, only to discover that was the problem. Putting the number first will fix the logic error as it will return an error when the code is run, which makes the error much easier to find.

I'm not sure if the above applies , as my experience with python is limited, but it is a good habit to get into.
DtY




PostPosted: Wed Apr 29, 2009 6:54 am   Post subject: RE:Evaluate my Code!

That's interesting, I never thought about that, it doesn't seem to be the case in python though:
Python:
>>> a = 2
>>> if a = 2:
  File "<stdin>", line 1
    if a = 2:
         ^
SyntaxError: invalid syntax
DemonWasp




PostPosted: Wed Apr 29, 2009 10:44 am   Post subject: RE:Evaluate my Code!

Maybe not an issue in Python (which refuses to cast the integer value to a boolean, apparently), but in languages like C, C++ and Java, that's valid syntax that means "if (assignment) not equal to zero", as zero is the value for false.

Java throws a warning when you try this, C and C++ usually compile without warning or error.
Sponsor
Sponsor
Sponsor
sponsor
Zeroth




PostPosted: Wed Apr 29, 2009 2:56 pm   Post subject: Re: RE:Evaluate my Code!

DemonWasp @ Wed Apr 29, 2009 7:44 am wrote:
Maybe not an issue in Python (which refuses to cast the integer value to a boolean, apparently), but in languages like C, C++ and Java, that's valid syntax that means "if (assignment) not equal to zero", as zero is the value for false.

Java throws a warning when you try this, C and C++ usually compile without warning or error.


Wrong. Try doing an while(1): or whatever. It does cast to a boolean, just not directly.

No, what happens is that C/C++ allows assignments within statements. Python does not allow those. You can work around that with list comprehensions and lambas... but really, do you want to?
wtd




PostPosted: Wed Apr 29, 2009 3:01 pm   Post subject: Re: RE:Evaluate my Code!

Clayton @ Mon Apr 20, 2009 1:38 am wrote:
What happens if you want to add a fourth candidate to your election? Coding a program to dynamically accept any combination/number of possibilities is a very good habit to get in to.


Seconded.

Just because the assignment calls for only three options, doesn't mean your code should be inflexible.
wtd




PostPosted: Wed Apr 29, 2009 3:01 pm   Post subject: Re: RE:Evaluate my Code!

Clayton @ Mon Apr 20, 2009 1:38 am wrote:
What happens if you want to add a fourth candidate to your election? Coding a program to dynamically accept any combination/number of possibilities is a very good habit to get in to.


Seconded.

Just because the assignment calls for only three options, doesn't mean your code should be inflexible.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: