-2

Below is my first python program. I am trying idea not yet covered in class as i hate staying stagnant and want to solve issues that may arise if i were to just use the info we've learned in class. As for my question, the program works but what were be ways to condense the code, if any? Thanks!

#This is a program to provide an itemized receipt for a campsite
# CONSTANTS
ELECTRICITY=10.00 #one time electricity charge

class colors:
    ERROR = "\033[91m"
    END = "\033[0m"


#input validation 
while True:
    while True:
        try:
            nightly_rate=float(input("Enter the Basic Nightly Rate:$"))
        except ValueError:
            print(colors.ERROR +"ERROR: Please Enter the Dollar Amount"+colors.END)
        else:
            break
    while True:
        try:
            number_of_nights=int(input("Enter the Number of Nights You Will Be Staying:"))
        except ValueError:
            print(colors.ERROR +"ERROR: Please Enter a Number"+colors.END)
        else:
            break
    while True:
        try:
            campers=int(input("Enter the Number of Campers:"))
        except ValueError:
            print(colors.ERROR +"ERROR: Please Enter a Number"+colors.END)  
        else:  
            break
    break
#processing         
while True:
    try:
        campsite=nightly_rate*number_of_nights              
        tax=(ELECTRICITY*0.07)+(campsite*0.07)
        ranger=(campsite+ELECTRICITY+tax)*0.15 #gratuity paid towards Ranger
        total=campsite+ELECTRICITY+tax+ranger  #total paid per camper
        total_per=total/campers
    except ZeroDivisionError:                  #attempt to work around ZeroDivisionError
        total_per=0                            #total per set to zero as the user inputed 0 for number-
    break                                          #-of campers

#Output     #Cant figure out how to get only the output colored
print("Nightly Rate-----------------------",nightly_rate)
print("Number of Nights-------------------",number_of_nights)
print("Number of Campers------------------",campers)
print()
print("Campsite--------------------------- $%4.2f"%campsite)
print("Electricity------------------------ $%4.2f"%ELECTRICITY)
print("Tax-------------------------------- $%4.2f"%tax)
print("Ranger----------------------------- $%4.2f"%ranger)
print("Total------------------------------ $%4.2f"%total)
print()
print("Cost Per Camper-------------------  $%4.2f"%total_per)
Phantom
  • 9
  • 2
  • 3
    https://codereview.stackexchange.com/ – ddejohn Sep 22 '21 at 04:39
  • You might want to read: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Matthias Sep 22 '21 at 07:36

2 Answers2

2
  1. You can remove both the outer while loops, as break is seen there at the top level, so the loops runs once only.
  2. You can convert colors to an Enum class if desired (this is more of a stylistic choice tbh)
  3. The line tax=(ELECTRICITY*0.07)+(campsite*0.07) can be represented as x*0.07 + y*0.07, which can be simplified to 0.07(x+y) or 0.07 * (ELECTRICITY + campsite) in this case.
  4. Instead of manually padding the - characters in the print statements, you can use f-strings with simple formatting trick. For example, try this out:
width = 40
fill = '-'
tax = 1.2345

print(f'{"Tax":{fill}<{width}} ${tax:4.2f}')
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53
2
  1. The else in try statement is unnecessary. You can just put the break in the the end of the try statement. REF

  2. In the end, in the print statements, I recommend you to use another types of formatting. You can use '...{}..'.format(..) or further pythonic is f'...{val}'. Your method is the oldest. More ref

MarceloBaliu
  • 230
  • 1
  • 10