在Python3中使用列表和if语句对begginer进行While循环
我对编码(Python)是新手,正在尝试学习循环。我在处理一些复杂的while和for循环时遇到了一些困难。这里我尝试创建一个函数并使用while循环。我可以得到一些关于如何修复此代码的想法,并得到一些关于我做错了什么的解释吗 我试图用这段代码实现的是,我在一个列表中存储了一些数字,这些数字是秘密的。直到用户没有输入其中一个数字,循环才会继续询问。一旦用户输入其中一个数字,循环将优先退出,而不使用sys中的exit()在Python3中使用列表和if语句对begginer进行While循环,python,python-3.x,Python,Python 3.x,我对编码(Python)是新手,正在尝试学习循环。我在处理一些复杂的while和for循环时遇到了一些困难。这里我尝试创建一个函数并使用while循环。我可以得到一些关于如何修复此代码的想法,并得到一些关于我做错了什么的解释吗 我试图用这段代码实现的是,我在一个列表中存储了一些数字,这些数字是秘密的。直到用户没有输入其中一个数字,循环才会继续询问。一旦用户输入其中一个数字,循环将优先退出,而不使用sys中的exit() def hell_hole(): print("You have just
def hell_hole():
print("You have just fallen through the hell hole.")
print("You must guess the right number to stop falling otherwise this program will keep repeating.")
print("The right numbers are between 1 - 10 ")
password = [4,9,8]
while True:
typed_in = input("What is the passing code?\n> ")
if typed_in != password:
print("Wrong, try again!")
elif typed_in == password:
print("Well done! You have stopped falling.")
else:
print("Say what?")
我知道,如果我将if语句更改为以下内容,这个问题可以解决:
while True:
typed_in = input("\nWhat is the passing code?\n> ")
if "4" in typed_in or "8" in typed_in or "9" in typed_in:
print("Well done! You have stopped falling.")
exit()
else:
print("Wrong, try again!")
但是如果可能的话,我想尝试修复初始代码。而不是sys.exit()
您可以使用break
来中断循环(这里是while
循环),也可以使用从函数返回的return
请注意,您可以通过使用return yourValue
从函数中返回一个值以使用它,但在您的情况下,它没有用处
另外,另一个有用的控制流关键字是continue
,它允许您跳过循环的迭代。所有这些关键字都适用于,而和则适用于循环
为了使您的if
语句更好,我认为您应该检查密码是否是您想要的值之一:
如果在[“4”、“8”、“9”]中键入:
或者检查输入的字符串中是否有这些值之一,就像您所做的那样:
如果有(在[“4”、“8”、“9”]中为x键入x):
您可以在
语句中使用:
password=['4'、'9'、'8']
尽管如此:
键入\u in=input(“\n传递的代码是什么?\n>”)
如果键入了\u密码:
打印(“干得好!你已经停止跌倒了。”)
打破
其他:
打印(“错误,再试一次!”)
注意:您正在尝试将输入(字符串)与整个列表进行比较:
如果输入了_!=密码
相反,检查输入是否在列表中
检查列表中是否存在值的最快方法:
如果在密码中键入_:
在您的列表(密码=[4,9,8])中还有整数,input()返回字符串
因此,您需要将输入转换为整数:
int(输入(“传递的代码是什么?\n>”)
返回语句可以用作一种控制流。通过在函数中放置一个(或多个)return语句,return语句允许您终止函数的执行
password = [4,9,8] # List of numbers
while True:
typed_in = int(input("What is the passing code?\n> "))
if typed_in not in password: # Check if input is IN the list
print("Wrong, try again!")
elif typed_in in password: # Check if input is NOT in the list
print("Well done! You have stopped falling.")
return # Exit function
else:
print("Say what?")
您将在下面找到您的代码的工作版本!
由于它已经发布,如果您想检查用户输入的号码是否在密码列表中,您可以使用中的关键字来执行此操作。此外,由于密码是整数,您需要将输入转换为这种类型
为了退出while循环,您可以使用break,它允许您退出更嵌套的循环
希望有帮助
def hell_hole():
print("You have just fallen through the hell hole.")
print("You must guess the right number to stop falling otherwise this program will keep repeating.")
print("The right numbers are between 1 - 10 ")
password = [4,9,8]
while True:
typed_in = int(input("What is the passing code?\n> "))
if typed_in in password:
print("Well done! You have stopped falling.")
break
else:
print("Wrong, try again!")
- 将用户输入强制转换为
int
,因为您要将其与int
数组进行比较
- 使用
中的和
中的检查输入的号码是否在
密码中
使用break
而不是exit()
<代码>中断
将在
循环时退出
工作执行:
while True:
typed_in = int(input("What is the passing code?\n> "))
if typed_in not in password:
print("Wrong, try again!")
elif typed_in in password:
print("Well done! You have stopped falling.")
break
else:
print("Say what?")
演示:而不是exit()
尝试break
“我知道如果我更改if语句,这个问题可以解决……但是如果可能,我想尝试修复初始代码。”我不明白;如果你不想做出改变来解决问题,那么你所说的修复是什么意思?您只是想更好地理解为什么原始代码不起作用吗?还是什么?是的,先生!这正是我的观点!谢谢你把它清理干净!:)<代码>如果键入_!=密码:
typed_in
是一个str
,其中作为密码
是一个列表
,条件将始终是True
。替换为:typed_in=int(输入(“传递的代码是什么?\n>”)
可能这不是他想要的。他可能只想在输入的字符串中输入4、9或8,就像他的代码一样。他的代码在typed_in中有“4”或“8”在typed_in或“9”在typed_in与其他答案的注释相同。他的代码在typed_in中有“4”或“8”在typed_in或“9”在typed_in他想修复初始代码,其中:password=[4,9,8]@NenriI不是在说这个。你读过他的密码吗?好的,你正在解决2个问题,但不是他职位上的3个问题,而不是告诉他应该做什么,解释他应该做什么。请记住,他是Python新手(同样,与其他答案一样,您的代码并没有复制他的注释)