如何解决在遵循某些步骤后更改值的布尔值
现在,我似乎无法弄清楚
我想的是一个全局布尔值,除非第二个门被访问,否则它将被设置为 False 然后它会将布尔值翻转为 True 但因为我正在调用 roomChoice() 返回进入二号房后的开始,一切都在重新开始。 只是想知道实现这一目标的逻辑。
尽量忽略这段代码的可怕混乱哈,我做这个程序是为了尝试从Java转换到Python并在这个过程中学习Vim,所以在这里慢慢学习。
global key = False
def roomChoice():
print("It is now time to make a decision,so go on,1,2 or 3")
userinput = int(input())
while userinput != 1 or 2 or 3:
if(userinput == 1):
print("Doors locked")
roomChoice()
elif(userinput == 1 and key == True):
print(
"You've entered the room,it's now time to decide if you want to steal the wallet?")
userinput = input()
if(userinput == "yes"):
print("Nice move,quick lets get out of here")
elevator()
elif(userinput == "no"):
print("Well,on your way back to the elevator someone sees you coming out of the apartment,later they realise a wallet has been stolen,because you have been seen at the crime you are falsely imprisoned for burglary. 10 years,the judge really didn’t like you.")
exit()
else:
roomChoice()
elif(userinput == 2):
print("Oh look a key,I wonder if that'll get you into room one?")
key = True
roomChoice()
else:
roomChoice()
break
解决方法
EDIT:您只是在第一个 if/elif 语句中出现逻辑错误,您必须检查 if 语句中的键是否仍然为 false,否则它将始终执行而不是 elif在它下面。
您是否尝试将键声明为函数内的全局变量?
key = False
def roomChoice():
global key
userinput = int(input("It is now time to make a decision,so go on,1,2 or 3: ")) # you can pass the prompt string as a parameter to the input method
while userinput != 1 or userinput != 2 or userinput != 3:
if(userinput == 1 and key is False): # key has not yet been found
print("Doors locked")
roomChoice()
elif(userinput == 1 and key is True): # now key has been found
print()
userinput = input("You've entered the room,it's now time to decide if you want to steal the wallet? ")
if(userinput == "yes"):
print("Nice move,quick lets get out of here")
elevator()
elif(userinput == "no"):
print("Well,on your way back to the elevator someone sees you coming out of the apartment,later they realise a wallet has been stolen,because you have been seen at the crime you are falsely imprisoned for burglary. 10 years,the judge really didn’t like you.")
exit()
else:
roomChoice()
elif(userinput == 2):
print("Oh look a key,I wonder if that'll get you into room one?")
key = True
roomChoice()
else:
roomChoice()
break
,
我想通了,这些有趣的答案之一,我只需要翻转 if & elif,因为它满足 if 语句的要求,所以它永远不会进入 elif 部分。不错。
global key = False
def roomChoice():
print("It is now time to make a decision,2 or 3")
userinput = int(input())
while userinput != 1 or 2 or 3:
if(userinput == 1 and key == True):
print(
"You've entered the room,it's now time to decide if you want to steal the wallet?")
userinput = input()
if(userinput == "yes"):
print("Nice move,the judge really didn’t like you.")
exit()
else:
roomChoice()
elif(userinput == 1):
print("Doors locked")
roomChoice()
elif(userinput == 2):
print("Oh look a key,I wonder if that'll get you into room one?")
key = True
roomChoice()
else:
roomChoice()
break
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。