asked 40.5k views
4 votes
Need Python for this program You will write a program that calculates and prints the bill for a cellular telephone company. A telephone company offers two types of service: regular and premium. Its rate varies, depending on the type of service. The rates are compared as follows. Regular service: $10.00 plus the first 50 minutes are free. Charges for over 50 minutes are $0.20 per minute. Premium service: $25 plus: For calls made from 6:00 a.m. to 6:00 p.m., the first 75 minutes are free; charges for more than 75 minutes are $0.10 per minute For calls made from 6:00 pm to 6:00 a.m., the first 100 minutes are free; charges for more than 100 minutes are $0.05 per minute. Your program should prompt the user to enter an account number, a service code (type character), and the number of minutes the service was used. A service code of r or R means regular service; a service code of p or P means premium service. Treat any other character as an error. Your program should output the account number, type of service, number of minutes the telephone service was used, and the amount due from the user. For the premium service, the customer may be using the service during the day and the night. Therefore, to calculate the bill, you must ask the user to input the number of minutes the service was used during the day and the number of minutes the service was used during the night. Your program should validate user input for negative values and loop until the user enters the correct entry. Also, using a loop make sure to ask if the user wants to continue with the program or not. If the user enters YES, the program should continue else, the program will exit.

asked
User Efemoney
by
8.1k points

1 Answer

3 votes

Answer:

def calculate_bill(service_code, minutes_day, minutes_night):

if service_code in ['r', 'R']: # Regular service

total_minutes = minutes_day + minutes_night

if total_minutes <= 50:

return 10.00

else:

excess_minutes = total_minutes - 50

return 10.00 + (excess_minutes * 0.20)

elif service_code in ['p', 'P']: # Premium service

total_minutes_day = minutes_day

total_minutes_night = minutes_night

if total_minutes_day > 75:

excess_minutes_day = total_minutes_day - 75

else:

excess_minutes_day = 0

if total_minutes_night > 100:

excess_minutes_night = total_minutes_night - 100

else:

excess_minutes_night = 0

return 25.00 + (excess_minutes_day * 0.10) + (excess_minutes_night * 0.05)

else:

return -1 # Invalid service code

def main():

while True:

account_number = input("Enter account number: ")

service_code = input("Enter service code (R for Regular, P for Premium): ").strip().lower()

if service_code not in ['r', 'p']:

print("Invalid service code. Please enter R for Regular or P for Premium.")

continue

try:

minutes_day = float(input("Enter the number of minutes used during the day: "))

minutes_night = float(input("Enter the number of minutes used during the night: "))

if minutes_day < 0 or minutes_night < 0:

print("Minutes cannot be negative. Please enter valid values.")

continue

bill_amount = calculate_bill(service_code, minutes_day, minutes_night)

if bill_amount != -1:

print("\\Account Number:", account_number)

print("Service Type:", "Regular" if service_code == 'r' else "Premium")

print("Minutes Used (Day):", minutes_day)

print("Minutes Used (Night):", minutes_night)

print("Amount Due: $", format(bill_amount, '.2f'))

else:

print("Invalid service code. Please enter R for Regular or P for Premium.")

except ValueError:

print("Invalid input. Please enter valid numeric values.")

another_transaction = input("Do you want to continue (YES/NO)? ").strip().lower()

if another_transaction != 'yes':

break

if __name__ == "__main__":

main()

Step-by-step explanation:

answered
User Maninekkalapudi
by
7.9k points