Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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上的用户输入重新启动我的程序?_Python - Fatal编程技术网

根据Python上的用户输入重新启动我的程序?

根据Python上的用户输入重新启动我的程序?,python,Python,我是编程新手,仅供参考。我希望我的程序根据用户输入的内容重新启动到顶部。如果用户输入2个名称,它将继续。如果他们输入一个或两个以上的名字,它应该重新启动程序,但我不知道如何做到这一点 def main(): print("Hello, please type a name.") first_name, last_name = str(input("")).split() while input != first_name + last_name: prin

我是编程新手,仅供参考。我希望我的程序根据用户输入的内容重新启动到顶部。如果用户输入2个名称,它将继续。如果他们输入一个或两个以上的名字,它应该重新启动程序,但我不知道如何做到这一点

def main():
    print("Hello, please type a name.")
    first_name, last_name = str(input("")).split()
    while input != first_name + last_name:
        print("Please enter your first name and last name.")
main()

在指定之前,应使用while循环并检查拆分的长度:

def main():
    while True:
        inp = input("Please enter your first name and last name.")
        spl = inp.split()
        if len(spl) == 2: # if len is 2, we have two names
            first_name, last_name = spl 
            return first_name, last_name # return or  break and then do whatever with the first and last name
使用

好吧,你的程序一开始对我不起作用,所以简单地解析名字和姓氏,我建议:

f, l = [str(x) for x in raw_input("enter first and last name: ").split()]
def main():
  print “type your first & last name”
  try:
    f, l = [str(x) for x in raw_input("enter first and last name: ").split()]
    if f and l:
      return f + ‘ ‘+ l
  except:
    main()
而且,如果你在没有好的'ol ctrl+c的情况下运行它,你的while循环将像是打破你的生活。因此,我建议:

f, l = [str(x) for x in raw_input("enter first and last name: ").split()]
def main():
  print “type your first & last name”
  try:
    f, l = [str(x) for x in raw_input("enter first and last name: ").split()]
    if f and l:
      return f + ‘ ‘+ l
  except:
    main()
except:main()将在出现错误时为您重新运行程序