Python I';我正在尝试创建一个“是”或“否”的对话树

Python I';我正在尝试创建一个“是”或“否”的对话树,python,Python,这是我第一次发帖,如果这是一场灾难,我深表歉意。我试图自学代码,并希望创建一个“是”或“否”对话树。它应该提示用户回答问题,如果答案不是“是”或“否”,则应重新打印问题以供使用。如果答案是“是”,则应打印“是”1响应并结束序列。如果输入为否,则应进入下一个问题。并继续,直到打印出yes1[3]或no1[4]响应,结束整个序列。 相反,无论您输入什么响应,或者如果您按Enter键,它都会继续。继续下一个问题,直到最后一个问题。然后它开始打印所有的yes1和No 1回复。一旦全部打印出来,它就会在最

这是我第一次发帖,如果这是一场灾难,我深表歉意。我试图自学代码,并希望创建一个“是”或“否”对话树。它应该提示用户回答问题,如果答案不是“是”或“否”,则应重新打印问题以供使用。如果答案是“是”,则应打印“是”1响应并结束序列。如果输入为否,则应进入下一个问题。并继续,直到打印出yes1[3]或no1[4]响应,结束整个序列。 相反,无论您输入什么响应,或者如果您按Enter键,它都会继续。继续下一个问题,直到最后一个问题。然后它开始打印所有的yes1和No 1回复。一旦全部打印出来,它就会在最后一次输入的无限循环中运行

感谢您的帮助。多谢各位

    #have tried with and without this yes or no bool
yes = True

yes == True

no = False

no == False

#str

#str prints the question but negates user input

#continues to next question if Enter is press

#as well as any other input.

#0
quest0 = ('Could I please have a cookie? \n')
can = (input(quest0))

#1
quest1 = ("please?\n")
i = (input(quest1))

#2
quest2 = ('just one?\n')
please = (input(quest2))

#3
quest3 = ("what if i never ask for another cookie again?\n" +
          ".......until tomorrow.\n")
have = (input(quest3))



#list y/n
#replies print after all questions are asked and do not follow sequence
#have tried with and without the input
#if replies are yes
yes1 = [
    #0
    (input("sure, here you go.")),
    #1
    (input("ok but just half.")),
    #2
    (input("ok sure, but after dinner.")),
    #3
    (input("sure, might have one too.")),
    #4
    (input("why not, it's been a day")),
]
#if replies are no
#have tried this with and without the input
no1 = [
    #0
    ("Not today honey",
     #1
     (input("no, you'll spoil your dinner.")),
     #2
     (input("you had 2 yesterday, not today hun")),
     #3
     (input("we're all out, we'll get some at the store later")),
     ]

    #initial quest 0
    #should take initial input to start seuqence
    #it does print
    print(str(can))

    #y/n loop for can: instead it moves on to next like regardless of input
    while (str(can)) != (yes or no):
    print(str(can))
    #once yes or no complied move on
    while (str(can)) == (yes or no):
        print(str(can))
    continue


    #prints yes1 #0
    if (str(can)) == yes:
        print(yes1[0])
        break
        #should end quest 0 sequence

    #1  should take user no input and print quest 1
    elif (str(can)) == no:
        print(str(can))
        while (str(can)) == no:
            print(str(i))
        continue
        #continues to quest 1

    #1        #quest 1 y/n loop
        while (str(i)) != (yes or no):
            print(str(i))
            #when yes or no complied begin next sequence
            while (str(i)) == (yes or no):
                print(str(i))
            continue
            #continues with quest 1

            #should print yes1 #1 and end sequence
            if (str(i)) == yes:
                print(yes1[1])
                break
                #sequence should end here

            #if no print quest 2
            elif (str(i)) == no:
                print(str(please))
            continue
            #continues to quest 2

            #quest 2 y/n loop
            while (str(please)) != (yes or no):
                print(str(please))
                #when yes or no complied begin next sequence
            while(str(please)) == (yes or no):
                print(str(please))
                continue
    #continue to next sequence

    #1              #should print yes1 #2
                if (str(please)) == yes:
                    print(yes1[2])
                    break
                    #sequence ends here


                #should print quest 3
                elif (str(please)) == no:
                    print(str(have))
                continue

                    #quest 3 y/n loop
                while (str(have)) != (yes or no):
                    print(str(have))
                    #if yes or no complied move on
                    while (str(have)) == (yes or no):
                        print(str(have))
                    continue
                    #should continue

                    # prints yes1 #3
                    if (str(have)) == yes:
                        print(yes1[3])
                        break
                        #sequence should end here

                    #should print no1 #4
                    elif (str(have)) == no:
                        print(no1[4])
                    break
                    #entire sequence should  end here
                    #doesnt end here instead it 
                     continues to
                     print all yes1 and no1 str if i
                     continue to hit Enter and then goes 
                     off
                     in an infinite loop repeating last
                     input

首先,更改此事件的每次发生:

x != (yes or no)
为此:

x != yes and x != no
或为此:

x not in [yes, no]
尝试以下方法

yes = "yes"
no = 'no'

questions = ['Could I please have a cookie?',
             'please?',
             'just one?',
             'what if i never ask for another cookie again?\n.......until tomorrow']

yeses = ["sure, here you go.",
          "ok but just half.",
          "ok sure, but after dinner.",
          "sure, might have one too.",
          "why not, it's been a day"]

nos = ['no way',
      "not today honey",
      "no, you'll spoil your dinner.",
      "you had 2 yesterday, not today hun",
      "we're all out, we'll get some at the store later"
     ]

for i, question in enumerate(questions):
  while True:
    # loop until they respond with yes or no
    answer = input(question).lower()
    if answer != yes and answer != no:
      print('answer yes or no please')
      continue  # continue while loop since wasn't yes/no
    break       # discontinue while loopo

  if answer == yes:
    print(yeses[i])  # output i-th yes response
    break
  else:
    print(nos[i])    # output i-th no response
else:
  print("Sorry, you don't get a cookie")

你不必要地过度使用了参数,例如:(input(“当然,给你”)、(str(i))、(str(请))等等。简化为:input(“当然,给你”)、str(i)、str(请)等等。我对continue语句的位置感到困惑。要么你的缩进是错误的,要么你有很多“死代码”。@DarrylG我以为你用了continue,只要你想在条件满足后转到下一行。我弄错了吗?@DarrylG谢谢你,我现在就实施。真的,我绝望了,试着看看我是否用错了。谢谢你澄清这一点up@DiarrheaisGodsModernWrath--是的,continue的用法不同。当在控制块中使用时,如
for或while
,它会随着块的下一次迭代而重新启动,从下面的点跳过代码。