Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.4.3中的循环_Python - Fatal编程技术网

Python 3.4.3中的循环

Python 3.4.3中的循环,python,Python,我为我的无知提前道歉,但我试图用python编写一些东西,需要向用户提问,然后用户做出响应。根据该回答,程序应打印回答并重复问题,直到提供正确答案。我正在使用Python 3.4.3 print("Enter Password") password = input("Enter Password: ") if password == 'Hello': print("Enter Name") else: print("Wrong Password") name = input("

我为我的无知提前道歉,但我试图用python编写一些东西,需要向用户提问,然后用户做出响应。根据该回答,程序应打印回答并重复问题,直到提供正确答案。我正在使用Python 3.4.3

print("Enter Password")
password = input("Enter Password: ")
if password == 'Hello':
    print("Enter Name")
else:
    print("Wrong Password")

name = input("Type your name, please: ")

发生的事情是,即使我没有输入“你好”,它仍会继续,不会再问问题,打印错误的密码,然后输入你的名字,请。。。。我错过了什么?请再次感谢您,我很抱歉,我对这一点非常陌生。

您没有使用循环,因此代码是按顺序执行的。在这里使用
while
循环似乎是最好的

while True:
  print("Enter Password")
  password = input("Enter Password: ")
  if password == 'Hello':
    break
  else:
    print("Wrong Password")

name = input("Type your name, please: ")

代码中没有循环。您有一个条件(
if/else
),但没有循环。循环类似于语句或语句

password = input("Enter Password: ")
while password != "Hello":
    print("Wrong Password")
    password = input("Enter Password: ")
name = input("Type your name, please: ")

这将循环直到您的
password
变量等于
Hello
(大写很重要!)

这将循环直到输入“Hello”。您需要使用循环:
for
while

只是添加了while而不是if,并翻转了相应的操作

print("Enter Password")
password = input("Enter Password: ")
while password != 'Hello':
    print("Wrong Password")
    password = input("Enter Password: ")
else:
    print("Enter Name")
    name = input("Type your name, please: ")
见: