开发者社区 问答 正文

为什么我的项目不允许用户退出程序?

简介: 我试图做一个用户的退出行输入0作为退出程序的输入,但我有一个困难的时间来实现这段代码。我已经厌倦了许多方法,如导入系统和系统退出,但似乎没有什么是工作。我正在努力使这个程序运行有效,易于使用,没有一个错误 谢谢!

input_exit = (input('Press 0 to exit the program: ')
if input_exit == '0'
    print('Thank you for choosing Lit Rentals')
    exit

我的代码:

print(" ______________________________________________")
print("|                                              |")
print("|              <3   Lit Rentals    <3          |")
print("|    Update: Class A, B, and C are availble    |")
print("|                  Instructions                |")
print("|   1.) Enter your full name (no numbers!)     |")
print("|   2.) Enter the Truck class you like to rent |")
print("|   3.) Enter the days you have rented for ONLY|")
print("|   4.) Enter your starting mileage (NUMBERS!) |")
print("|   5.) Enter your ending mileage (NUMBERS!)   |")
print("|   6.) Review your bill and enter 0 when done |")
print("|   7.) If not done or wanting to start the    |")
print("|   program again with the number 1            |")
print("|______________________________________________|")

enterName = (input('Please enter your full name: ')).upper()
try:
    classCode = (input('Please enter your rental classification code (Class A, Class B, Class C): ')).upper()
except:
    print("Please enter the correct letter and try again")
    classCode = (input('Please enter your rental classification code (Class A, Class B, Class C): ')).upper()
try:
    days_rented = int(input('How many days did you rent your vehicle for? '))
except:
    print("Error! You enterted the wrong input, please give a number.")
    days_rented = int(input('How many days did you rent your vehicle for? '))
try:
    initOdometer = int(input('What was your vehicle\'s initial odometer reading (in Miles)? '))
except:
    print("Error! Please neter the correct number for mileage and try again.")
    initOdometer = int(input('What was your vehicle\'s initial odometer reading (in Miles)? '))
try:
    finOdometer = int(input('What was your vehicle\'s final odometer reading (in Miles)? '))
except:
    print("Error! Please enter the final mileage in numbers and try again.")
    finOdometer = int(input('What was your vehicle\'s final odometer reading (in Miles)? '))
# Final odometer reading
# Formula for miles driven during rental period:

miles_driven = finOdometer - initOdometer

# Calculations based on the three possible class codes:

from decimal import Decimal

classifications = ['A','B','C']
baseChargeDaily = [Decimal('17.95'), Decimal('27.95'), Decimal('37.95')]
baseChargeWeekly = [Decimal('107.59'), Decimal('166.59'), Decimal('237.99')]
MileageCharge = [Decimal('0.49'), Decimal('0.69'), Decimal('0.79')]
slot = 0

if classCode == 'A': 
    slot = 0

elif classCode == 'B': 
    slot = 1

elif classCode == 'C': 
    slot = 2



def charge_calc(slot, days_rented, miles_driven):
    w, d = divmod(days_rented, 7)
    total_charge = d *baseChargeDaily[slot]
    total_charge += w *baseChargeWeekly [slot]
    if w > 0:
        miles_driven = miles_driven -200
    total_charge = total_charge + miles_driven * MileageCharge [slot]
    return total_charge
# call the function and assign the returned values
final_bill= charge_calc(slot, days_rented, miles_driven)

# Code for final output statements:
if classCode == 'A' or classCode == 'B' or classCode == 'C':
    print('\nSummary:')
    print()
    print('Name of vehicle enter: %s' % enterName)
    print('Number of days vehicle rented: {}'.format(days_rented))
    print('Initial odometer reading on rented vehicle: {} miles'.format(initOdometer))
    print('Final odometer reading on rented vehicle: {} miles'.format(finOdometer))
    print('Number of miles driven during rental period: %d' % (miles_driven))
    print('\n Final billing cost: $%.2f' % final_bill)

input_exit = input('Press 0 to exit the program or 1 to restart: ')
if input_exit == '0':
    print('Thank you for choosing Lit Rentals')
    exit(0)
elif input_exit == '1':
    getInput(enterName)
    continue

问题来源StackOverflow 地址:/questions/59383085/why-can-my-project-not-allow-a-user-to-exit-the-program

展开
收起
kun坤 2019-12-27 11:23:43 412 分享 版权
1 条回答
写回答
取消 提交回答
  • 你有一些语法错误,但主要的问题是退出调用

    input_exit = input('Press 0 to exit the program: ')
    if input_exit == '0':
        print('Thank you for choosing Lit Rentals')
        exit(0)
    elif input_exit == '1':
        getInput()
    

    关于你的更新:

    from decimal import Decimal
    
    def charge_calc(slot, days_rented, miles_driven, baseChargeDaily, baseChargeWeekly, MileageCharge):
            w, d = divmod(days_rented, 7)
            total_charge = d *baseChargeDaily[slot]
            total_charge += w *baseChargeWeekly [slot]
            if w > 0:
                miles_driven = miles_driven -200
            total_charge = total_charge + miles_driven * MileageCharge [slot]
            return total_charge
    
    def application_flow():
        print(" ______________________________________________")
        print("|                                              |")
        print("|              <3   Lit Rentals    <3          |")
        print("|    Update: Class A, B, and C are availble    |")
        print("|                  Instructions                |")
        print("|   1.) Enter your full name (no numbers!)     |")
        print("|   2.) Enter the Truck class you like to rent |")
        print("|   3.) Enter the days you have rented for ONLY|")
        print("|   4.) Enter your starting mileage (NUMBERS!) |")
        print("|   5.) Enter your ending mileage (NUMBERS!)   |")
        print("|   6.) Review your bill and enter 0 when done |")
        print("|   7.) If not done or wanting to start the    |")
        print("|   program again with the number 1            |")
        print("|______________________________________________|")
    
        enterName = (input('Please enter your full name: ')).upper()
        try:
            classCode = (input('Please enter your rental classification code (Class A, Class B, Class C): ')).upper()
        except:
            print("Please enter the correct letter and try again")
            classCode = (input('Please enter your rental classification code (Class A, Class B, Class C): ')).upper()
        try:
            days_rented = int(input('How many days did you rent your vehicle for? '))
        except:
            print("Error! You enterted the wrong input, please give a number.")
            days_rented = int(input('How many days did you rent your vehicle for? '))
        try:
            initOdometer = int(input('What was your vehicle\'s initial odometer reading (in Miles)? '))
        except:
            print("Error! Please neter the correct number for mileage and try again.")
            initOdometer = int(input('What was your vehicle\'s initial odometer reading (in Miles)? '))
        try:
            finOdometer = int(input('What was your vehicle\'s final odometer reading (in Miles)? '))
        except:
            print("Error! Please enter the final mileage in numbers and try again.")
            finOdometer = int(input('What was your vehicle\'s final odometer reading (in Miles)? '))
        # Final odometer reading
        # Formula for miles driven during rental period:
    
        miles_driven = finOdometer - initOdometer
    
        # Calculations based on the three possible class codes:
    
        classifications = ['A','B','C']
        baseChargeDaily = [Decimal('17.95'), Decimal('27.95'), Decimal('37.95')]
        baseChargeWeekly = [Decimal('107.59'), Decimal('166.59'), Decimal('237.99')]
        MileageCharge = [Decimal('0.49'), Decimal('0.69'), Decimal('0.79')]
        slot = 0
    
        if classCode == 'A': 
            slot = 0
    
        elif classCode == 'B': 
            slot = 1
    
        elif classCode == 'C': 
            slot = 2
    
        # call the function and assign the returned values
        final_bill= charge_calc(slot, days_rented, miles_driven, baseChargeDaily, baseChargeWeekly, MileageCharge)
    
        # Code for final output statements:
        if classCode == 'A' or classCode == 'B' or classCode == 'C':
            print('\nSummary:')
            print()
            print('Name of vehicle enter: %s' % enterName)
            print('Number of days vehicle rented: {}'.format(days_rented))
            print('Initial odometer reading on rented vehicle: {} miles'.format(initOdometer))
            print('Final odometer reading on rented vehicle: {} miles'.format(finOdometer))
            print('Number of miles driven during rental period: %d' % (miles_driven))
            print('\n Final billing cost: $%.2f' % final_bill)
    
        input_exit = input('Press 0 to exit the program or 1 to restart: ')
        if input_exit == '0':
            print('Thank you for choosing Lit Rentals')
            exit(0)
        elif input_exit == '1':
            application_flow()
    
    application_flow()
    

    getInput不是一个预定义的方法。在这种情况下,如果希望能够“重新启动”程序流,则需要理解重新启动的含义。在本例中,您希望允许用户再次插入数据。因此,实现此目的的一种方法是将所需的流封装到一个方法中,并在希望重新启动它时简单地调用它。

    2019-12-27 11:23:52
    赞同 展开评论
问答分类:
问答地址: