对于第一位,当我打印出“ask2”时,它打印出“exit”,而不是它应该打印的车牌。你知道吗
ask = input("-Would you like to 1 input an existing number plate\n--or 2 view a random number\n1 or 2: ")
ask2 = ""
plate = ""
if int(ask) == 1:
ask2 = ""
print("========================================================================")
while ask2 != 'exit':
ask2 = input ("Please enter it in such form (XX00XXX): ").lower()
# I had no idea that re existed, so I had to look it up.
# As your if-statement with re gave an error, I used this similar method for checking the format.
# I cannot tell you why yours didn't work, sorry.
valid = re.compile("[a-z][a-z]\d\d[a-z][a-z][a-z]\Z")
#b will start and end the program, meaning no more than 3-4 letters will be used.
# The code which tells the user to enter the right format (keeps looping)
# User can exit the loop by typing 'exit'
while (not valid.match(ask2)) and (ask2 != 'exit'):
print("========================================================================")
print("You can exit the validation by typing 'exit'.")
time.sleep(0.5)
print("========================================================================")
ask2 = input("Or stick to the rules, and enter it in such form (XX00XXX): ").lower()
if valid.match(ask2):
print("========================================================================\nVerification Success!")
ask2 = 'exit' # People generally try to avoid 'break' when possible, so I did it this way (same effect)
**print("The program, will determine whether or not the car "+str(plate),str(ask)+" is travelling more than the speed limit")**
此外,我正在寻找一些好的代码,是好的附加(把数据在一个列表),并打印。 这就是我所做的
while tryagain not in ["y","n","Y","N"]:
tryagain = input("Please enter y or n")
if tryagain.lower() == ["y","Y"]:
do_the_quiz()
if tryagain==["n","N"]:
cars.append(plate+": "+str(x))
print(cars)
打印
ask2
时,它会打印“exit”,因为您使用ask2 = 'exit'
将其设置为exit,并且在ask2
设置为“exit”之前,循环无法终止。你知道吗您可以使用
ask2
作为用户的输入,使用另一个变量loop
来确定何时退出循环。例如:我不太清楚您的另一个代码块试图实现什么,但是您测试
tryagain
的方式是不正确的,它永远不会等于一个两元素列表,例如["y","Y"]
,也许您打算使用in
?,此更改显示了至少解决该问题的一种方法:相关问题 更多 >
编程相关推荐