Feedback for a complete beginner

This is the place for queries that don't fit in any of the other categories.

Feedback for a complete beginner

Postby dses » Wed Sep 25, 2013 12:41 am

Ok so after reading a couple of python tutorials I've decided to just start programming what ever I could. The first thing I thought of was a program that could simulate flipping a coin. Even though it's a very simple program I'll take any kind of feedback no matter how harsh. So please be completely honest. :D

Code: Select all

"""This program is a simple coin flipper that shows what whether you got
heads or tails and asks if you want to flip again.
"""

#Coin.py

import random

class coin:

    def __init__(self):
       heads = 0
       tails = 0

    def flip(self):
        result = random.randint(0,1)
        if result == 0:
            print("Heads")
        elif result == 1:
            print("Tails")


def main():

    print("This is a coin flipper program.")
    input("To start press ENTER:")
   
    coin1 = coin()

    def flipCoin():
        coin1.flip()
        answer = input("Do you want to flip again?(y/n):")
        if answer == "y":
            flipCoin()
        elif answer == "n":
            print("Ok, Goodbye")     

    flipCoin()
   
main()



I've also made another version of this program that tracks results of all coin flips but I'm having trouble with the tracker as the values always stay at zero. :?:

Code: Select all
"""This program is a simple coin flipper that shows what whether you got
heads or tails and asks if you want to flip again.
"""
### Version 1.1
### This version now keeps track of all results.

#Coin_1.1.py

import random

class coin:
   

    def __init__(self):
        heads = 0
        tails = 0

    def flip(self):
        result = random.randint(0,1)
        if result == 0:
            print("Heads")
            heads =+ 1
        elif result == 1:
            print("Tails")
            tails =+ 1
        print("Heads:", heads,"Tails:",tails)
   


def main():

    print("This is a coin flipper program.")
    input("To start press ENTER:")
   
    coin1 = coin()
   
    def flipCoin():

        coin1.flip()
        answer = input("Do you want to flip again?(y/n):")
        if answer == "y":
            flipCoin()
        elif answer == "n":
            print("Ok, Goodbye") 

    flipCoin()
   
main()
Last edited by micseydel on Wed Sep 25, 2013 1:22 am, edited 1 time in total.
Reason: First post lock.
dses
 
Posts: 1
Joined: Wed Sep 25, 2013 12:23 am

Re: Feedback for a complete beginner

Postby micseydel » Wed Sep 25, 2013 1:41 am

I'm generally quite pro-OOP, but especially for your first set of code tags, you definitely don't need it. You never use the values set in __init__() either. Also, it's somewhat strange to define a function inside of main(). Typically unless there's good reason otherwise, all functions are declared in the global scope. Here, the "good" reason you might have for it is to use the coin instance, but having a scope silently reach up into a higher scope like that isn't usually done either.

As for the second version, I still don't think you need OOP. You're not creating multiple instances, you're not encapsulating anything, you're not inheriting, and the data stored inside of it is generic enough that there isn't really any need to associate it tightly with the coin.

This more like how I'd do it
Code: Select all
import random

HEADS = 0
TAILS = 1

def main():
    print("This is a coin flipper program.")
    input("To start press ENTER:")

    heads = 0
    tails = 0
    while True:
        result = random.randint(0,1)
        if result == HEADS:
            print("Heads")
            heads =+ 1
        elif result == TAILS:
            print("Tails")
            tails =+ 1

        print("Heads:", heads, "Tails:", tails)

        answer = input("Do you want to flip again? (y/n): ")
        if not answer.lower().startswith("y"):
            print("Ok, Goodbye")
            break

if __name__ == "__main__":
    main()

I could see writing a do_continue() function which returns True or False in the main loop, and continually prompts the user until they enter either "y" or "n" strictly. It would be before or after main() depending on your preference (I like main() at the very top, personally). You could also skip the constants at the top and just use "else" instead of "elif" but I like that it's self-documenting and very semantic. It's not terribly important though.

On this forum, we like to use PEP 8 too. If you decide not to, whatever you do do, should be consistent (not that you weren't here, I just thought it worth mentioning).
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1372
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests