计算电话费(python)

时间:2016-08-23 03:14:46

标签: python-3.x

尝试返回数据问题是用户输入的数字不正确。

如果输入的号码不正确,我需要知道如何再次回答问题

$.ajax({
type:"POST",
url:"account_ing",
data:$("#editform_account input:checkbox").serialize(),
cache:false,
success:function(data){
$("#loading").html(data);
}
});

1 个答案:

答案 0 :(得分:1)

您应该输入一个无限循环,并且只有在用户输入有效输入时才会中断。

import math

costmap = {1: 30, 3: 45, 6: 60}
price = float(input('Please enter the price of the phone: '))
datamsg = 'Enter the amount of the data you will use, 1 gig, 3 gigs, 6 gigs: '

while True:

    try:
        data = int(input(datamsg))
    except ValueError:
        print('must input a number')
        continue

    tax = price * .0925
    total = price + tax

    print('If your phone cost {}, the after tax total in Tennessee is {}'
          .format(price, round(total, 2)))

    try:
        datap = costmap[data]
        print('The price of one gig is ${}.'.format(datap))
        break

    except KeyError:
        print('{} is not an option try again.'.format(data))

pmt = total / 24
bill = (datap + 20) * 1.13
total_bill = bill + pmt

print('With the phone payments over 24 months of ${}'.format(round(pmt, 2)))
print('and data at ${} a month and line access of $20.'.format(datap))
print('Your total cost after tax is ${}'.format(round(total_bill, 2)))

您还可以通过定义dict将输入映射到成本值来简化if/else子句。如果该值不存在,则抛出一个异常,您可以捕获该异常,发出错误并再次循环。

最后,我建议您尝试通过pep-8检查程序运行代码,例如http://pep8online.com/。它将教您如何格式化代码以使其更易于阅读。目前,您的代码更难以阅读。