代码不打印

2024-09-29 23:15:15 发布

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

对于第一位,当我打印出“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)

Tags: orthetoininputifexitit
1条回答
网友
1楼 · 发布于 2024-09-29 23:15:15

打印ask2时,它会打印“exit”,因为您使用ask2 = 'exit'将其设置为exit,并且在ask2设置为“exit”之前,循环无法终止。你知道吗

您可以使用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)

相关问题 更多 >

    热门问题