如何重复我的代码,但下次从变量中删除一个?

2024-10-02 08:28:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个python代码,就好像我要把它放在停车罚单机上一样。它完成了所有功能-显示可用停车位的数量,询问您的车牌的最后三位数字,让您插入钱,但我不确定在我重复代码时如何从可用车位的数量中减去1。另外,在执行此操作时如何重复代码?这是我的密码:

#Import
import time

#Variables:
spaces=100
ticketprice=3
cashin=0
totalcash=0
engineer='egg'

while True:


    #Beginning:
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    print("Harry's Car Park") 
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    print("There are currently", spaces,"spaces available")
    reg=input("Please enter the last three digits of your registration plate > ")


    #Engineer Mode
    if reg==engineer:
        print()
        print("YOU ARE NOW IN ENGINEER MODE")
        print()
        ticketprice=input("What would you like to change the ticket price to? ")
        print("Thank you! The ticket price has been changed to £", ticketprice)
        print()
        print("YOU ARE NO LONGER IN ENGINEER MODE")
        print()


    #Paying:
    print("Please insert £"+(str(ticketprice)+" to pay for your ticket:"))

    while totalcash<int(ticketprice):
        cashin=int(input("Enter pound coins here £"))
        totalcash=totalcash+cashin

        print()

        print("Thank you")

        print("Amount entered=£",totalcash)


    #Tickets
    if totalcash>int(ticketprice):
        print("Your change is £",totalcash-int(ticketprice))
        print()
        print("You have paid the full amount!")
        print()
        print("Printing Parking Ticket...")
        print()
        time.sleep(1)
        print("     ****************************************************")
        time.sleep(0.5)
        print("     *                        Harry's Car Park                      ")
        time.sleep(0.5)
        print("     *                                                                          ")
        time.sleep(0.5)
        print("     *                        Ticket Price: £",(int(ticketprice)),"                   ")
        time.sleep(0.5)
        print("     * 16/01/2020     Reg Plate:",(reg),"        17:03   ")
        time.sleep(0.5)
        print("     ****************************************************")

        print()


    if totalcash==int(ticketprice):
        print("You have paid the full amount!")
        print()
        print("Printing Parking Ticket...")
        print()
        time.sleep(1)
        print("     ****************************************************")
        time.sleep(0.5)
        print("     *                        Harry's Car Park                      ")
        time.sleep(0.5)
        print("     *                                                                          ")
        time.sleep(0.5)
        print("     *                        Ticket Price: £",(int(ticketprice)),"                      ")
        time.sleep(0.5)
        print("     * 16/01/2020     Reg Plate:",(reg),"        17:03      ")
        time.sleep(0.5)
        print("     ****************************************************")

        print()

Tags: theto代码timesleepregticketcar
2条回答

若只在最后一个区块(当你们打印票证时)你们使用空格,那个该怎么办;减少空间? 您还应该初始化它,以便它存储最大可用空间量

每当客户支付票证金额时,只需写空格-=1,然后添加一个条件,如果空格==0,则退出

相关问题 更多 >

    热门问题