2016-09-28 25 views
0

當我運行這個功能時,一切都很好。我可以將x設置爲90,將y設置爲9並將z設置爲10.但是,將y設置爲.9將不起作用。分割時,請保持ValueError。

請幫忙。

def div(): 

    x = int(input('Number? ')) 
    y = int(input('Number? ')) 

    if x == 0 or y == 0: 
     print('0') 
    else: 
     z = (x/y) * 1.0 
     print(z) 

回答

1

這是因爲您將輸入轉換爲int值。字符串'0.9'不是一個文字,所以int('0.9')引發了一個ValueError異常。

如果你想處理漂浮你需要:

def div(): 

    x = float(input('Number? ')) 
    y = float(input('Number? ')) 

    if x == 0 or y == 0: 
     print('0') 
    else: 
     z = (x/y) * 1.0 
     print(z) 
+0

感謝,奧利弗!它與浮點函數一起工作。 – 11swallowedinthesea