【问题标题】:How do i code it so if the user enters no the while loop continues but if the user enters yes the program stop, beginner我如何编写代码以便如果用户不输入 while 循环继续但如果用户输入是程序停止,初学者
【发布时间】:2023-02-23 22:18:29
【问题描述】:
rooms = ["Front Door", "Living Room", "Kitchen", "Study", "Storage", "Basement", "Master    Bedroom", "Bathroom", "Attic", "Garden"]

desc = ["Was it too scary?", "The TV is on, what can you find?", "It smells so bad!", "The Master    did all his work here...", "It is so dark and so cold in here... Why is it so frosty?", "The Master has his secrets...", "Don't worry its just a bathroom!", "Rubbish and Antiques", "Beautiful, is it not?"]

roomLIT = [True, True, True, True, False, False, False, False, True]

roomLocked = []

username = input("Welcome Player! What is your username?")
age = input("What is your age?")

rn = int(input("Enter Room Number:"))
print("You have entered the", rooms[rn])
print(desc[rn])
rn = 1
leave = "yes"
while leave == "yes":
    while rn != 0:
        rn = int(input("Enter Room Number:"))
        print(desc[rn])
    leave = input("Do you want to leave?") 

问题出在 while 循环中

问题在最后

【问题讨论】:

  • 只问专业的。我猜这是一个作业,你需要学习和学习,我给个提示。要获得所需的行为,您可以更改外部 while 循环的条件以检查用户是否键入了“no”。如果答案为“否”,则继续循环并要求用户输入不同的房间号。如果用户说“是”,循环结束,程序结束。

标签: python while-loop


【解决方案1】:

在你的代码中,你有

leave = "yes"
while leave == "yes":
    while rn != 0:
        rn = int(input("Enter Room Number:"))
        print(desc[rn])
    leave = input("Do you want to leave?") 

在那里,您正在设置变量leave = "yes"while leave == "yes"。想想这句话的意思,你是说玩家一到房间就想离开,只要说"yes",他们就会留在房间里。尝试将其默认设置为"no",并检查玩家在询问他们是否要离开时是否没有说"yes"。这应该让玩家留在房间里。

您也可以简单地将问题 "Do you want to leave?" 改写为 "Do you want to stay in the room?"。如果您这样做,我还建议将变量 leave 重命名为 stay,以便阅读您的代码的人更容易理解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-28
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多