Okay, I started learning Python last weekend on Saturday with some tutorials and a book.
With this being my first programming language my program won't amaze you :P
The program I made is a calculator for working out how much turf a person would need given the length and the width of the area. It can add an extra 5% extra for 'Trimming & Wastage' and it suggests an amount after rounding up the total number.

print "*************************"
print "Turf Calculator v1.0.0"
print "*************************"

import math

turf = True

while turf:
    print "\n-------------------------\n"
    length = input("Enter Length: ")
    print "\n-------------------------\n"
    width = input("Enter Width: ")
    print "\n-------------------------\n"
    first = length * width
    second = first / 9.0
    total = second / 1.196
    add = raw_input('Add 5% for Trimming and Wastage? (Y/N): ')
    print "\n-------------------------\n"
    if add != 'y' and add != 'Y':
            print "Total: " + "%.2f" % total + " Rolls\n"
            print "Suggested Amount: " + `math.ceil(total)`
            print "\n-------------------------\n"
    elif add == 'y' or 'Y':
            final = total * 0.05
            last = total + final
            print "Total: " + "%.2f" % last + " Rolls\n"
            print "Suggested Amount: " + `math.ceil(last)` + " Rolls"
            print "\n-------------------------\n"
    end = raw_input('Re-run Program? (Y/N): ')
    if end != 'y' and end != 'Y':
            turf = False

Let me know what i could improve, add, remove or whatever else you think i should know.

One thing i would like to change if i can is, when they say yes to re-run the program, if it could completely reset and not leave previous results, if anybody understands what i mean.

Anyway, let me know what you think or if i missed anything :)

Cheers

Recommended Answers

All 8 Replies

See this link for the same question.

Thanks,

I took a look at the link and had a mess around with it but was only able to get it to do what it is already doing, which is re-running the program but leaving previous results behind.

I'm really new to this so no doubt I will have done something wrong.

You enter all of the values each time so they would be whatever was entered for each while() loop. Note that if something other than "y" is entered for trimming and wastage, the total will not be calculated. It is simpler to read the code if it is in the following form:

if add != 'y' and add != 'Y':
#
#     use instead
    if add.lower() != 'y':
#
#     or
    if add in ['y', "Y"]:
#
# 
    trim_waste = 0.05
    add = raw_input('Add 5% for Trimming and Wastage? (Y/N): ')
    print "\n-------------------------\n"
    if add.lower() != 'y':
        trim_waste = 0.0
    last = total + (total * trim_waste)

Thanks for that I have tidied my code up a little bit now, although i still can't get it to restart the program fully without it showing previous results, but I'll sort that out eventually :)

There is an error here (from the original code) so use add.lower() instead:

elif add == 'y' or 'Y':
#     This breaks down into
    elif add == 'y'
# or
    elif 'Y'     #which is always true

"i still can't get it to restart the program fully without it showing previous results"
Do you mean that you want the screen to erase the prior input and calculated results? That is tricky if you do it by clearing the screen, but you can do something very simple: At line 33 put this: print('\n'*80) .

You will also want to move the lines that print the header inside the loop. You could use triple quotes:

print("""****************************
  Turf Calculator 0.0.9
****************************""")

or, as an exercise, think about how to write a printHeader(title) function. Hint: The trick I showed with 80 newlines can work for asterisks too, and you can use a calculated value such as bar = '*'*(4+len(title)) .

"i still can't get it to restart the program fully without it showing previous results"
Do you mean that you want the screen to erase the prior input and calculated results? That is tricky if you do it by clearing the screen, but you can do something very simple: At line 33 put this: print('\n'*80) .

You will also want to move the lines that print the header inside the loop. You could use triple quotes:

print("""****************************
  Turf Calculator 0.0.9
****************************""")

or, as an exercise, think about how to write a printHeader(title) function. Hint: The trick I showed with 80 newlines can work for asterisks too, and you can use a calculated value such as bar = '*'*(4+len(title)) .

In linux (or ANSI capable terminal), clear the screen with print("\x1B[2J") .

@woooee - Thanks, I have went through my code and used your suggestions to tidy it up/fix it a little, thanks again!

@griswolf - Yeah i wanted it to erase everything that had been entered, as if you had just opened the program again. I'll give what you said a shot, thanks! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.