如何根据两个菜单选项进行计算?

时间:2017-04-27 00:23:59

标签: python function menu arguments parameter-passing

Instruction

假设是图像,但我不确定你是否能看到它?

我必须向用户提供一个菜单,以确定哪个样式卡车然后使用另一个菜单,以便他们按周或每天选择费率。然后我必须得到多少天/周以及卡车行驶了多少英里。必须根据两个菜单选项及其输入计算所有信息。我如何说A和1然后等于....我知道如何使用if语句与一个变量,但我不知道如何合并所有这些输入来有效地计算。 请指教。下面是我到目前为止的编码。

def main():
    print('Please select from the following rental truck options')
    firstMenu()
    print("Please Tell us how many Days/Weeks you have rented our truck and how many miles were driven")
    milesTime()

def firstMenu():
    menu = input("Please bhoose from the following options(A, B, C, D)and press enter:\n"
                 "A = Class A Truck\n"
                 "B = Class B Truck\n"
                 "C = Class C Truck")
     if menu == str('A'):
         secondMenu()
     elif menu == str('B'):
         secondMenu()
     elif menu == str('C'):
         secondMenu()

    firstMenu()

def secondMenu():
    print('Please enter code for Daily or Weekly Rate')
    print("1 = Daily")
    print("2 = Weekly")
    option2 = input()
    secondMenu()

def milesTime():
    miles = input("Please Enter number of Miles Driven")
    time = input("Please Enter the number of days/weeks the truck was rented")
    milesTime()

def calculate():
    while firstMenu == 'A':

1 个答案:

答案 0 :(得分:0)

def main():
    while True:
        menu()
        print('*' * 80)

def menu():
    class_list = ['A', 'B', 'C']
    user_class = input("Please bhoose from the following options(A, B, C, D)and press enter:\n"
                 "A = Class A Truck\n"
                 "B = Class B Truck\n"
                 "C = Class C Truck\n")
    if user_class not in class_list:
        raise ValueError('invalid classification!')
    else:
      user_class = class_list.index(user_class)
    print('Please enter code for Daily or Weekly Rate')
    print("1 = Daily")
    print("2 = Weekly")
    rent_type = input()
    if rent_type not in ['1', '2']:
        raise ValueError('invalid rental type')
    miles_time(user_class, rent_type)

def miles_time(user_class, rent_type):
    data = [
      [19.95, 0.59, 125.68],
      [29.95, 0.79, 188.68],
      [39.95, 0.85, 251.68]
    ]
    miles = int(input("Please Enter number of Miles Driven\n"))
    time = int(input("Please Enter the number of days/weeks the truck was rented\n"))
    user_data = data[user_class]
    user_rental = 0
    if rent_type == '1':
        user_rental = user_data[0] * time + miles * user_data[1]
    else:
        if miles <= time * 200:
            user_rental = user_data[2] * time
        else:
            user_rental = user_data[2] * time + (miles - 200) * user_data[1]
    print(user_rental)


if __name__ == '__main__':
    main()

尚未完全测试。但是两个给定的测试用例通过了。