Python 如何修复';其他';输出多个结果

Python 如何修复';其他';输出多个结果,python,Python,这是一个非常基本的问题,如果一个数字可以被3/5/both/none整除,那么尝试输出结果,但当它们不为真时,将返回2条语句。我该如何解决这个问题 我试着移动else缩进的位置,第一次它不会输出不是3或5的倍数的数字,第二次它会输出两个答案 while True: z = input("Please enter a number- to end the program enter z as -1 ") if z % 3 == 0 and z % 5 ==0: pr

这是一个非常基本的问题,如果一个数字可以被3/5/both/none整除,那么尝试输出结果,但当它们不为真时,将返回2条语句。我该如何解决这个问题

我试着移动else缩进的位置,第一次它不会输出不是3或5的倍数的数字,第二次它会输出两个答案

while True:
    z = input("Please enter a number- to end the program enter z as -1 ")
    if z % 3 == 0 and z % 5 ==0:
        print("Your number is a multiple of 3 and 5")
    elif z % 3 == 0 and z % 5 != 0:
        print("Your number is a multiple of 3")
    elif  z % 3 != 0 and z % 5 ==0:
        print("Your number  is a multiple of 5")
    if z == -1:
        break
   else:
       print("Your number is not a multiple of 3 or 5")

即,如果输入67,则您的号码不是预期的3或5的倍数。但是,如果输入15,则您的号码是3和5的倍数,
您的号码不是3或5的倍数,
是意外输出。

通过启动新的
if
块,您将结束前面的
elif

if z == -1:
    break
else:
    print("Your number is not a multiple of 3 or 5")
正如@Daniel Junglas评论所说,你应该这样构造它:

z = input("Please enter a number- to end the program enter z as -1 ")
if z == -1:
    break
elif (z % 3 == 0) and (z % 5 == 0):
    print("Your number is a multiple of 3 and 5")
elif (z % 3 == 0) and (z % 5 != 0):
    print("Your number is a multiple of 3")
elif (z % 3 != 0) and (z % 5 == 0):
    print("Your number  is a multiple of 5")
else:
    print("Your number is not a multiple of 3 or 5")
while True:
    z = input("Please enter a number- to end the program enter z as -1 ")
    # cast to int
    z = int(z)
    # break early
    if z == -1:
        break
    elif z % 3 == 0 and z % 5 == 0:
        print("Your number is a multiple of 3 and 5")
    elif z % 3 == 0:
        print("Your number is a multiple of 3")
    elif z % 5 == 0:
        print("Your number is a multiple of 5")
    else:
        print("Your number is not a multiple of 3 or 5")

如果您将目前为止所有的评论建议合并在一起,您会得到如下结果:

z = input("Please enter a number- to end the program enter z as -1 ")
if z == -1:
    break
elif (z % 3 == 0) and (z % 5 == 0):
    print("Your number is a multiple of 3 and 5")
elif (z % 3 == 0) and (z % 5 != 0):
    print("Your number is a multiple of 3")
elif (z % 3 != 0) and (z % 5 == 0):
    print("Your number  is a multiple of 5")
else:
    print("Your number is not a multiple of 3 or 5")
while True:
    z = input("Please enter a number- to end the program enter z as -1 ")
    # cast to int
    z = int(z)
    # break early
    if z == -1:
        break
    elif z % 3 == 0 and z % 5 == 0:
        print("Your number is a multiple of 3 and 5")
    elif z % 3 == 0:
        print("Your number is a multiple of 3")
    elif z % 5 == 0:
        print("Your number is a multiple of 5")
    else:
        print("Your number is not a multiple of 3 or 5")

您还需要通过
int(z)
键入cast输入。当前它是一个字符串。您可能希望
z==-1
测试成为第一个测试。这样,代码的其余部分就可以正确区分这些情况(假设你将
else
elif
对齐。你太晚了相关标记:@Error syntacticalreforme我猜OP使用的是Python 2。否则,他们会得到一个异常,而不仅仅是一个重复的输出谢谢你的帮助,为什么中断部分去哪里很重要?它不重要。它只会帮助你布局代码和代码。)意图。我将其添加到“评论建议摘要”中更重要的是,您对-1的检查并不构成对3和5个倍数的检查的一部分,因此,两个检查都会运行。在中断的情况下移动-1检查意味着您不必再浪费宝贵的CPU周期先检查多个。这是有意义的-因此,如果有中断测试,它应该总是先进行吗?@ChristopherBurt一般来说,您是这样认为的希望对测试进行排序,以使您所做的工作最少:进行廉价测试,并可能提前通过测试。这取决于测试的语义:例如,只有第二个测试通过时,一个测试才可能是安全的,因此这限制了两个测试的运行顺序。您忘记强制转换为int:
z=int(输入(…)
Whoops,实际上OP似乎在使用Python 2,因为他们没有收到错误。因此他们应该使用
z=int(原始输入(…)
好的,它在Python 2中不适用于我,所以我将它省略,因为它对问题的答案并不重要。