首页 > 解决方案 > 将用户返回到上一行

问题描述

我试图让代码根据用户提供的输入将用户返回到前一行代码。如果他们回答“不”或“不是真的”等,当我问他们是否理解规则时,我希望他们回到前面的代码行并再次阅读。

print("Ok, the rules of this 'game' are very simple; You are kind of doing a virtual house tour.")
print("You will be inspecting the house for its condition and to see if it is 'sellable'.")
print("You will be going from room to room and checking it out based on the room description I give.")
print("You will be giving each room a number value of 0,1,2, or 3.  A 0 takes away from the value of the house.")
print("A 1 doesn't change the value of the house.  A 2 or a 3 increase the value of the house.")
print("If at any point in the tour you think the house is not 'sellable', you just have to say so and the game ends.")
print("Your progress in the house will be tracked at all times.")
ans2=input("Understand? Are you ready?")
if ans2.lower() in Accept_ans:
    print ("Ok, great!  Lets begin the tour.")
else:
    print("ok, read the instructions again")

标签: python

解决方案


如果答案不符合预期,请使用while循环返回起点。

ok = 0
while not ok:
    print("Ok, the rules of this 'game' are very simple; You are kind of doing a virtual house tour.")
    print("You will be inspecting the house for its condition and to see if it is 'sellable'.")
    print("You will be going from room to room and checking it out based on the room description I give.")
    print("You will be giving each room a number value of 0,1,2, or 3.  A 0 takes away from the value of the house.")
    print("A 1 doesn't change the value of the house.  A 2 or a 3 increase the value of the house.")
    print("If at any point in the tour you think the house is not 'sellable', you just have to say so and the game ends.")
    print("Your progress in the house will be tracked at all times.")
    ans2=input("Understand? Are you ready?")
    if ans2.lower() in Accept_ans:
        print ("Ok, great!  Lets begin the tour.")
        ok = 1
    else:
        print("ok, read the instructions again")

推荐阅读