【问题标题】:I'm not getting the else output我没有得到其他输出
【发布时间】:2019-09-03 16:42:32
【问题描述】:

在测试一些东西时,我尝试了这个想法,但我无法让它发挥作用:

parx = input("Write your parX: ")
pary = input("Write your parY: ")

while pary != 0 and parx != 0:
    cociente = int(parx) / int(pary)
    print ("Su cociente es: ",cociente) 
    parx = input("Write your parX: ")
        pary = input("Write your parY: ")
else:
    print("your ordered pair is not divisible")

我期望 else 的输出,但是当我在我的变量上写 0,0 时它只显示错误,我希望当我输入 0 0 时程序说“你的有序对不可整除”

错误提示:

  File "Ejercicio2PDF3.py", line 6, in <module>
ZeroDivisionError: division by zero

【问题讨论】:

  • 请格式化您的问题..
  • input() 被当作字符串,'0' 不是0

标签: python python-3.x while-loop


【解决方案1】:

input 返回一个字符串,0 != "0"

要么立即解析你的字符串

parx = int(input("Write your parX: ")) 
pary = int(input("Write your parY: "))

或者检查字符串

while pary != "0" and parx != "0":

还要注意,您应该使用一些错误检查。如果用户输入一个非数字,你的程序就会崩溃。

【讨论】:

    【解决方案2】:

    只需要一个模组。 变化:

    while pary != 0 and parx != 0:
    

    收件人:

    while int(pary) != 0 and int(parx) != 0:
    

    最终代码:

    parx = input("Write your parX: ")
    pary = input("Write your parY: ")
    
    while int(pary) != 0 and int(parx) != 0:
        cociente = int(parx) / int(pary)
        print ("Su cociente es: ",cociente) 
        parx = input("Write your parX: ")
        pary = input("Write your parY: ")
    else:
        print("your ordered pair is not divisible")
    

    【讨论】:

      【解决方案3】:

      只需将strings 转换为int

      parx = int(input("Write your parX: "))
      pary = int(input("Write your parY: "))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-19
        • 1970-01-01
        相关资源
        最近更新 更多