This is program for figuring out cost of carpet at X length Y Width and Z cost/sqft (turned to yard). I am not very strong in looping yet, but I am pretty sure that is what has to be done here. Ideally at the end I want to ask the user if they want to try for another carpet. If yes, loop back up to the beginning of the program, if no, exit. I am very new to python just started it this semester for my first IT class. Any help would be greatly appreciated.

Length=int(input("Enter Length of Carpet in Feet "))
retries=4
while Length<=0 or Length>=100:
    print("Value out of Range.  Please Re-Enter A Length Between, But Not Including, 0 And 100")
    retries=retries-1
    if retries<0:
        raise IOError("Learn to Read Directions")
    Length=int(input("Enter Length of Carpet in Feet "))
else:
    print("Length Acceptable at ", Length)

print()
print()

Width=int(input("Enter Width of Carpet in Feet "))
retries2=4
while Width<=0 or Width>=200:
    print("Value out of Range.  Please Re-Enter A Length Between, But Not Including, 0 And 200")
    retries2=retries2-1
    if retries2<0:
        raise IOError("Learn to Read Directions")
    Width=int(input("Enter Length of Carpet in Feet "))
else:
    print("Width Acceptable at ", Width)
print()
print()
Area=((Length*Width)/9)
print("Area is ", Area, "Square Yards")
Cost=(int(input("Enter Price/Sq.Ft. Of Carpet "))*9)
TotCost=Area*Cost
if TotCost<=5000:
    print("I'll Purchase Selected Carpet")
else:
    print("Carpet Is Too Expensive")
    print()

Edited 6 Years Ago by Iron-Man1138: n/a

Put the above code in a function and then call the function from a loop. You can use one function to get the width or the length as well by passing the maximum allowed value and the literal to print, "Width" or "Length", to the function. Note that the Python Style Guide specifies variable names and function names should be all lower case.

ask = "y"
while ask.upper() == "Y":
    calculate_costs()     ## calls above code as a function

    ask = raw_input("Would you like to calculate another ('Y' or 'N')? ")

Edited 6 Years Ago by woooee: n/a

Thank you so much for your reply if there is anyone who would want to see this finished, here it is.

def carpetcost():
    Length=int(input("Enter Length of Carpet in Feet "))
    retries=4
    while Length<=0 or Length>=100:
        print("Value out of Range.  Please Re-Enter A Length Between, But Not Including, 0 And 100")
        retries=retries-1
        if retries<0:
            raise IOError("Learn to Read Directions")
        Length=int(input("Enter Length of Carpet in Feet "))
    else:
        print("Length Acceptable at ", Length)

    print()
    print()

    Width=int(input("Enter Width of Carpet in Feet "))
    retries2=4
    while Width<=0 or Width>=200:
        print("Value out of Range.  Please Re-Enter A Length Between, But Not Including, 0 And 200")
        retries2=retries2-1
        if retries2<0:
            raise IOError("Learn to Read Directions ")
        Width=int(input("Enter Length of Carpet in Feet "))
    else:
        print("Width Acceptable at ", Width)
    print()
    print()
    Area=((Length*Width)/9)
    print("Area is ", Area, "Square Yards")
    Cost=(int(input("Enter Price/Sq.Ft. Of Carpet "))*9)
    TotCost=Area*Cost
    if TotCost<=5000:
        print("I'll Purchase Selected Carpet")
    else:
        print("Carpet Is Too Expensive")
        print()
        retry="y"
        retry=input("Would you like to price another carpet 'y' for yes 'n' for no  ")
        if retry.upper()=="Y":
            carpetcost()
        else:
            print("Thank you come again")
            
            
carpetcost()

Edited 6 Years Ago by Iron-Man1138: n/a

Purpose of line 37? Please read well the meaning of the text wooee wrote, use function to take out repetion of code (and to handle the bad inputs including not numbers and out of range) not to do while loop with recursion.

I'll input price of l, what happens? Price of -100? Because you have not prepared function, you repeated the code two times and left it out from third place. Please, use functions (including parameters!)

P.S. It is quite possible that that same function you can use in 10 or 40 of your next programs, maybe worth the effort?

Edited 6 Years Ago by pyTony: n/a

This question has already been answered. Start a new discussion instead.