![](/img/trans.png)
[英]I'm getting same output of “Nodding” in first if else condition instead of “Smiling”. Could anyone tell me what can I do to fix it?
[英]I'm not getting the else output
雖然我正在測試一些東西,但我嘗試了這個想法,但我無法讓它發揮作用:
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"
要么立即解析你的字符串
parx = int(input("Write your parX: "))
pary = int(input("Write your parY: "))
或檢查字符串
while pary != "0" and parx != "0":
還要注意,您應該使用一些錯誤檢查。 如果用戶輸入一個非數字,你的程序就會崩潰。
只需要一個模組。 改變:
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")
只需將string
s 轉換為int
:
parx = int(input("Write your parX: "))
pary = int(input("Write your parY: "))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.