0

该程序计算超速罚单费用

政策规定,每张罚单为 50 美元,超出限制的每英里每小时 5 美元,超过 90 英里每小时的任何速度都将被罚款 200 美元。但是,如果速度不超过限速 5 英里/小时,则可以放弃该罚单。(例如,如果限速是 37,您可以在 41 时行驶,但不能在 42 时行驶。)程序会询问用户他们有多少张车票,并为每张车票询问限速和计时速度。我想计算并打印每张罚单的罚款,并返回所有罚款的总和,如果计时速度未超过限制或可以免除罚单,则报告罚单罚款 0 美元。

这是我的代码,它适用于单张票我需要帮助我为许多票证执行此操作,我是 python 新手。

def ask_limit():
    limit = float(input ("What was the speed limit? "))

    return limit
def ask_speed():
    speed = float(input ("What was your clocked speed? "))
    return speed
def findfine(speed, limit):
    if speed > 90:
        bigfine = ((speed - limit) * 5 + 250)
        print "your fine is", bigfine
    elif speed <= limit:
        print "you were traveling a legal speed"
    else:
        fine = ((speed - limit) * 5 + 50)
        print "your fine is", fine

def main():
    limit = ask_limit()
    speed = ask_speed()
    findfine(speed, limit)
main()
4

1 回答 1

3

Whenever you want to repeat something, consider using either a while or for loop. A while loop is for when you want to continuously repeat until some condition is True, and a for loop is for when you want to do something a set number of times. For example:

def main():
    done = False
    while not done:
        limit = ask_limit()
        speed = ask_speed()
        findfine(speed, limit)

        done = raw_input("Done? ") == "yes"

Notice I used another raw_input to make sure the user can stop the program.

Keep in mind that the limit and the speed is not remembered each time. As the program currently stands, it cannot return the sum total of the tickets. I'll leave it as an exercise to you to figure out the best way of doing so.

于 2013-09-17T16:11:38.307 回答