代码不打印我寻求的东西

时间:2015-11-18 05:20:37

标签: python python-3.x

对于第一位,当我打印出" ask2"时,它打印出"退出"而不是它应该打印的车牌。

     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)

1 个答案:

答案 0 :(得分:0)

当您打印ask2时,它会打印并退出'因为您将其设置为使用ask2 = 'exit'退出,并且在ask2设置为'退出'之前您的循环无法终止。

您可以将ask2用于用户的输入,使用另一个变量loop来确定何时退出循环。例如:

loop = True
while loop:
    # ...
    if valid.match(ask2) or ask2 == 'exit':
        loop = False

我不太确定你的其他代码块试图实现什么,但是你测试tryagain的方式不正确,它永远不会等于两个元素列表,例如["y","Y"] ,也许你打算使用in ?,这个改变显示了至少解决这个问题的一种方法:

while tryagain not in ["y","n","Y","N"]:
    tryagain = input("Please enter y or n")
if tryagain.lower() == "y":
    do_the_quiz()
else:
    cars.append(plate+": "+str(x))

print(cars)