[英]How do I repeat my code but take away one from a variable for the next time around?
I am making a python code as if I was going to put it in a car parking ticket machine.我正在制作一个 python 代码,就像我要把它放在停车场售票机一样。 It goes through all the functions - displays the number of available parking spaces, asks for the last three digits of your registration plate, gets you to insert money but I am unsure how to take 1 away from the amount of available spaces for when I repeat the code.它通过所有功能 - 显示可用停车位的数量,询问您车牌的最后三位数字,让您投入资金,但我不确定如何从可用车位数量中减去 1 重复时编码。 Also how do I repeat the code in doing so?另外我如何在这样做时重复代码? Here is my code:这是我的代码:
#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()
What if only into IF final block (when you print ticket) you use spaces--;如果只进入 IF 最后一个块(当你打印票时)你使用空格怎么办--; to reduce spaces?减少空间? Also you should initialize it so it stores the maximum amount of spaces available您还应该初始化它,以便它存储最大数量的可用空间
每当客户支付票款时,只需写空格-=1,然后添加一个条件,如果空格==0,则退出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.