Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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.x 如果我输入a为“否”,它会给出粗体的内容,两部分都是!。有人知道怎么解决这个问题吗?_Python 3.x - Fatal编程技术网

Python 3.x 如果我输入a为“否”,它会给出粗体的内容,两部分都是!。有人知道怎么解决这个问题吗?

Python 3.x 如果我输入a为“否”,它会给出粗体的内容,两部分都是!。有人知道怎么解决这个问题吗?,python-3.x,Python 3.x,所以。。。这就是我的问题:有人知道解决方法吗?它输出:好的,再试一次。和错误:你没有在推杆是或否!我真的很想解决这个问题。尝试制作一个基于文本的游戏,因为它们有时会很有趣,特别是有一个好的故事情节 # Infinite loop until yes or Yes is in-putted while True: # Ask name name = input("What is your name? ") if len(name) == 0: print("Inv

所以。。。这就是我的问题:有人知道解决方法吗?它输出:好的,再试一次。和错误:你没有在推杆是或否!我真的很想解决这个问题。尝试制作一个基于文本的游戏,因为它们有时会很有趣,特别是有一个好的故事情节

# Infinite loop until yes or Yes is in-putted
while True:
# Ask name
    name = input("What is your name? ")

    if len(name) == 0:
        print("Invalid name, cannot leave blank!")
    if len(name) > 0:
        a = input("Are you sure this is your name? (Yes/No) ")
# What yes/no/anything else does
    if a == "yes":
        print("Cool name", name)
        break
# HERE IS MY PROBLEM:
    if a == "No":
        print("Ok, try again then. ")

    if a == "Yes":
        print("Cool name", name)
        break

    if a == "no":
        print("Ok, try again then. ")
# AND HERE:
    else:
        print("ERROR: You did not input yes or no! ")
它输出:好的,再试一次

嗯。这是预期的行为,因为如果a==否:

和错误:你没有在推杆是或否

这也是意料之中的。这里

    if a == "no":
        print("Ok, try again then. ")
# AND HERE:
    else:
        print("ERROR: You did not input yes or no! ")
它不输入if,因为No不等于No。因此它输入else并打印错误

这应该可以解决它

if a == "No":
    print("Ok, try again then. ")
elif a == "Yes":
    print("Cool name", name)
    break
elif a == "no":
    print("Ok, try again then. ")
else:
    print("ERROR: You did not input yes or no! ")\

如果你也像这样,你可以检查no和no-in-one

if a == "no" or a == "No":
if a in ["no", "No"]
还是像这样

if a == "no" or a == "No":
if a in ["no", "No"]

最后一个else只是前一个if的else语句

将您的语句转换为elif,它将很好地工作——但这仍然不是最佳编码

在定义以下内容后,可以尝试此操作:

a.lower()
这使得答案用小写字母表示,因此您不需要单独测试“是”和“是”。它更简单,更像蟒蛇。您也可以使用or语句,如下所示:

if a == 'yes' or  a == 'Yes' or a == 'Y' or a == 'y': 
    # do stuff

谢谢这真的很有帮助!我用if a==no或a==no:来表示Yes和Yes。我是任何编程语言的初学者,15岁,这是非常令人困惑的。其中大部分是我自学的,还有我的计算机科学老师教给我的其他东西。谢谢!目前正在开发基于文本的游戏:/。我不知道如何做2d或3d世界设计,但你必须从某个地方开始。不过,这确实让事情变得更简单了。因为python是直接的xD。再次感谢!