Python:未知语法错误(简单)

时间:2013-11-25 08:39:30

标签: python

我在单词租赁方面遇到语法错误,我不知道我做错了什么。这就像我的第六个节目。任何和所有建议都有帮助。

#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)

4 个答案:

答案 0 :(得分:2)

您需要添加右下方代码段中所示的右括号,并确保条件的第一行与上一行对齐。另外,请考虑将rental的值与有效回复列表进行匹配 - 这是编写您提出的逻辑的更多Pythonic方式:

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"]):
或者,或者(但不是Pythonic):

if (cust_type == "R" or cust_type == "r"):

最后,请注意input("Were movies rented (Y or N): ")返回一个字符串,因此应该转换为整数。如果您使用int()进行投射,则会收到类型错误,而if rental in ['Y', 'y']:将永远不会评估为true

答案 1 :(得分:1)

if rental == 'Y' or rental == 'y':

应该解决这个问题

答案 2 :(得分:1)

以下应该可以帮到你。

rental = str(input("Were movies rented (Y or N): "))
if rental == "Y" or rental == "y":

zeantsoi提出的积分也有效。请考虑一下。

答案 3 :(得分:0)

有很多错误:

  • 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':
  • 整个if rental块需要缩进以与上一行对齐。
  • 以下两行需要尾随)

      

    connections = int(输入(“输入基本服务连接数:”)
      prem_channels = int(输入(“输入使用的付费频道数量:”)

  • if (connections块需要缩进以与上一行对齐。

  • if (cust_type == "R" or "r"):应为if cust_type == 'R' or cust_type == 'r':
  • bill_amt计算都需要使用prem_channels而不是prem_channel

此外,不需要围绕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)