简体   繁体   English

无限循环错误python 3

[英]Infinite loop error python 3

I am coding a simple chatbot for my computers class and I have run into a problem. 我正在为我的计算机课编写一个简单的聊天机器人,但遇到了问题。 I am trying to create a function that will ask for someone's name and then reply saying "Nice to meet you" and then their name. 我正在尝试创建一个函数,该函数将询问某人的名字,然后回复说“很高兴认识您”,然后回答他们的名字。 My function keep repeating "Hi Ii'm Bob. Whats your name?" 我的职能部门不断重复“嗨,我是鲍勃。你叫什么名字?” over and over again. 一遍又一遍地。 It works when its outside of the function but i cant figure out why it wont work when inside. 它在功能之外时起作用,但是我无法弄清楚为什么在内部时不起作用。

def hello():
    while True:
        print("Hi I'm Bob! What's your name?")
    name = input("Name:")
    print("Nice to meet you "+(name))

The while loop will always be true, so it never gets to your input - you need to indent the name/print to be within the while loop. while循环将始终为true,因此永远不会进入您的输入-您需要将名称/打印缩进到while循环内。

def hello():
    while True:
        print("Hi I'm Bob! What's your name?")
        name = input("Name:")
        print("Nice to meet you "+(name))

You should simply indent the question and answer into the loop: 您应该简单地将问题缩进循环中:

def hello():
    while True:
        print("Hi I'm Bob! What's your name?")
        name = input("Name:")
        print("Nice to meet you "+(name))    
def hello():
    while True:
        print("Hi I'm Bob! What's your name?")
        name = input("Name:")
        print("Nice to meet you " + name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM