简体   繁体   English

需要哪个循环以及如何合并它

[英]which loop is needed and how do i incorporate it

import time

Time_started=time.ctime()
Question1=input("Has vehicle gone past sensor 1 \n:")
if Question1=="yes":
    Confirmation1=input("Confirm? \n:")
elif Question1 !="yes":
    Question1=input("Has vehicle gone past sensor 1 \n:")
if Confirmation1=="yes":
    print ("Vehicle entered on",Time_started)
elif Confirmation1!="yes":
    Question1=input("Has vehicle gone past sensor 1 \n:")

Time_ended=time.ctime()
Question2=input("Has vehicle gone past sensor 2 \n:")
if Question2=="yes":
    Confirmation2=input("confirm? \n:")
elif Question2 !="yes":
    Question2=input("Has vehicle gone past sensor 2 \n:")
if Confirmation2=="yes":
    print("Vehicle left on",Time_ended) 
elif Confirmation !="yes":
    Question2=input("Has vehicle gone past sensor 2 \n:")

Here's what happened:这是发生的事情:

Has vehicle gone past sensor 1 
:yes
Confirm? 
:no
Has vehicle gone past sensor 1 
:yes
Has vehicle gone past sensor 2 
:yes
confirm? 
:

What should have happened:应该发生的事情:

The second time the program asked "has vehicle gone past sensor 1" and the user input was "yes" the program should have asked "confirm?"第二次程序询问“车辆是否经过传感器 1”并且用户输入“是”时,程序应该询问“确认”? but this didn't happen instead it went straight to the second question.但这并没有发生,而是直接进入了第二个问题。

I know that a loop is needed, however I do not know which loop and how to incorporate it.我知道需要一个循环,但是我不知道哪个循环以及如何合并它。

Just loop until the first confirmation is answered affirmatively:循环直到第一次确认得到肯定回答:

answer_to_confirmation_1 = None  # As long as we haven't asked,
                                 # there can't be an answer. ;-)
while answer_to_confirmation_1 != 'yes':
    answer_to_question_1 = input("Has vehicle gone past sensor 1 \n:")
    if answer_to_question_1 == 'yes':
        answer_to_confirmation_1 = input("Confirm? \n:")

# If we ever exit above loop, we can be sure of the following:
print('The user has confirmed that the vehicle has passed sensor 1.')

For putting input confirmation within a loop, you can see das-g's answer for your specific circumstance.要将输入确认放入循环中,您可以在特定情况下查看 das-g 的答案。 However, it looks like your code isn't quite doing what you want.但是,看起来您的代码并没有完全按照您的意愿行事。 If you're trying to set the time after the person enters their input, then you will need to call time.ctime() after input() returns.如果您尝试在用户输入他们的输入后设置时间,那么您将需要在 input() 返回后调用 time.ctime()。

Also, it looks like you're duplicating code by doing the same procedure twice.此外,看起来您正在通过执行相同的过程两次来复制代码。 I'd recommend creating a function instead that you can call multiple times, such as this:我建议您创建一个可以多次调用的函数,例如:

def confirm_time(prompt):
    while True:
        if input(prompt) == "yes":
            input_time = time.ctime()
            if input("Confirm time '{}'\n:".format(input_time)) == "yes":
                return input_time

Then you can call that function twice with the different prompts that you want, like this:然后,您可以使用所需的不同提示调用该函数两次,如下所示:

time_started = confirm_time("Enter yes when the vehicle passes sensor 1 \n:")
print("Vehicle entered on", time_started)
time_ended = confirm_time("Enter yes when the vehicle passes sensor 2 \n:")
print("Vehicle left on", time_ended)

Using that, I get the following output:使用它,我得到以下输出:

Enter yes when the vehicle passes sensor 1
:yes
Confirm time 'Sat Nov  7 19:04:33 2015'
:no
Enter yes when the vehicle passes sensor 1
:yes
Confirm time 'Sat Nov  7 19:04:41 2015'
:yes
Vehicle entered on Sat Nov  7 19:04:41 2015
Enter yes when the vehicle passes sensor 2
:yes
Confirm time 'Sat Nov  7 19:04:47 2015'
:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM