我正在为我的计算机类编写一个简单的聊天机器人,我遇到了一个问题。我正在尝试创建一个函数,它会询问某人的名字,然后回复说“很高兴认识你”,然后回答他们的名字。我的功能一直重复着“嗨,我是鲍勃,你叫什么名字?”一遍又一遍。它在函数的外部工作,但我不明白为什么它在内部不能工作。
def hello():
while True:
print("Hi I'm Bob! What's your name?")
name = input("Name:")
print("Nice to meet you "+(name))发布于 2017-11-07 00:17:31
您只需将问题和答案缩进循环中:
def hello():
while True:
print("Hi I'm Bob! What's your name?")
name = input("Name:")
print("Nice to meet you "+(name)) 发布于 2017-11-07 00:16:23
while循环将始终为true,因此它永远不会到达您的输入--您需要缩进名称/print才能在while循环中。
def hello():
while True:
print("Hi I'm Bob! What's your name?")
name = input("Name:")
print("Nice to meet you "+(name))发布于 2017-11-07 00:17:51
def hello():
while True:
print("Hi I'm Bob! What's your name?")
name = input("Name:")
print("Nice to meet you " + name)https://stackoverflow.com/questions/47147948
复制相似问题