我在单词rental上遇到语法错误,我根本不知道自己做错了什么。这就像是我的第六个节目。任何建议都是有用的。
#Computes Cable Bill
print("Welcome to cable customer billing calculator")
acct = int(input("Enter an account number"))
cust_type = input("Enter customer type: (R) Residential or (B) Business: ")
def residential():
processing = 4.50
basic_service = 20.50
daily_rental = 2.99
prem_channel_fee = 7.50
prem_channels = int(input("Enter the number of premium channels used: ")
rental = int(input("Were movies rented (Y or N): ")
if rental == Y or y:
rental_days = int(input("Enter the total number of rental days (one day for each movie, each day): ")
else:
rental_days = 0
bill_amt = processing + basic_service + (rental_days * daily_rental) + (prem_channels * prem_channel_fee)
return (bill_amt)
def business():
processing = 15
basic_service = 75
prem_channel_fee = 50
connections = int(input("Enter the number of basic service connections: ")
prem_channels = int(input("Enter the number of premium channels used: ")
if (connections <= 10):
bill_amt = processing + basic_service + (prem_channel * prem_channel_fee)
else:
bill_amt = processing + basic_service + ((connections - 10) * 5) + (prem_channel * prem_channel_fee)
return (bill_amt)
if (cust_type == "R" or "r"):
bill_total = residential()
else:
bill_total = business()
print("Account number: ", acct)
print("Amount due: ",bill_total)
您需要添加右括号,如下面的代码段所示,并确保条件语句的第一行与前一行对齐。此外,考虑将rental
的值与有效响应列表进行匹配——这是编写您提出的逻辑的更Python的方式:
prem_channels = int(input("Enter the number of premium channels used: "))
rental = int(input("Were movies rented (Y or N): "))
if rental in ['Y', 'y']:
rental_days = input("Enter the total number of rental days (one day for each movie, each day): ")
同样,以下几行也需要括号:
connections = int(input("Enter the number of basic service connections: "))
prem_channels = int(input("Enter the number of premium channels used: "))
如上所述替换您的最终条件的逻辑:
if (cust_type in ["R", "r"]):
或者,或者(但不太像Python):
if (cust_type == "R" or cust_type == "r"):
最后,请注意,input("Were movies rented (Y or N): ")
返回一个字符串,因此应该将而不是强制转换为整数。如果使用int()
进行强制转换,则会收到类型错误,并且if rental in ['Y', 'y']:
永远不会计算为true
。
if rental == 'Y' or rental == 'y':
应该解决这个
以下内容应该会对您有所帮助。
rental = str(input("Were movies rented (Y or N): "))
if rental == "Y" or rental == "y":
zeantsoi提出的积分也是有效的。也请考虑一下。
有许多错误:
prem_channels = int(input("Enter the number of premium channels used: ")
需要右括号rental = int(input("Were movies rented (Y or N): ")
移除int(
。输入是一个字符串if rental == Y or y:
应该是if rental == 'Y' or rental == 'y':
- 整个CCD_ 11块需要未凹陷以与前一行对齐
-
以下两行需要尾随
)
:connections=int(input("输入基本服务连接数:")
prem_channels=int(输入("输入使用的高级频道数:") -
if (connections
块需要未凹陷以与前一行对齐。 if (cust_type == "R" or "r"):
应为if cust_type == 'R' or cust_type == 'r':
- CCD_ 16的计算都需要使用CCD_ 17而不是CCD_
此外,if语句和返回值周围的括号是不必要的。
以下是您的代码与上述修复:
#Computes Cable Bill
print("Welcome to cable customer billing calculator")
acct = int(input("Enter an account number"))
cust_type = input("Enter customer type: (R) Residential or (B) Business: ")
def residential():
processing = 4.50
basic_service = 20.50
daily_rental = 2.99
prem_channel_fee = 7.50
prem_channels = int(input("Enter the number of premium channels used: "))
rental = input("Were movies rented (Y or N): ")
if rental == 'Y' or rental == 'y':
rental_days = int(input("Enter the total number of rental days (one day for each movie, each day): "))
else:
rental_days = 0
bill_amt = processing + basic_service + (rental_days * daily_rental) + (prem_channels * prem_channel_fee)
return bill_amt
def business():
processing = 15
basic_service = 75
prem_channel_fee = 50
connections = int(input("Enter the number of basic service connections: "))
prem_channels = int(input("Enter the number of premium channels used: "))
if connections <= 10:
bill_amt = processing + basic_service + (prem_channels * prem_channel_fee)
else:
bill_amt = processing + basic_service + ((connections - 10) * 5) + (prem_channels * prem_channel_fee)
return bill_amt
if cust_type == "R" or cust_type == "r":
bill_total = residential()
else:
bill_total = business()
print("Account number: ", acct)
print("Amount due: ",bill_total)