2012-09-13 44 views
4
print("enter start() to start the program") 

def start(): 
    print("This script converts GBP into any currency based on the exchange rate...") 
    print(" ") #enters a line 
    exchangeRate = int(input("Enter the exchange rate (Eg: 0.80)")) 
    print("how much would you like to convert???")      
    gpb = int(input()) 
    print(gpb*exchangeRate) 

十進制值和輸入£1它總是返回0我怎麼如果我把匯率在0.81打印在python

回答

7

使用float(),而不是int()input()電話。即,

gpb = float(input()) 

否則如果用戶輸入0.81int()將在轉換過程中截斷這0

通過使用float()您將保留提供的十進制值作爲輸入,並且您的計算應該產生您期望的結果。

0

您指定的類型爲int .....當您將1乘以0.81時,那些是整數(0,1,2,3,4,5,6,7,8 ....),得到0.81 .....整數的關鍵數字是在點之前的數字,在這種情況下是零。所以就像之前的答案一樣,簡單地說只是變量類型的變量。