2014-01-29 81 views
-1

我對Python很陌生,我想知道如何解決這個問題。我正在嘗試爲我的計算課程的學校項目製作貨幣轉換器,但是我收到錯誤消息。Python獲取類型錯誤

下面的代碼:

## Currency conversion calculator Mk.2 ## 

# USD, JPY, EUR 
GBP = ["1.66","169.14","1.21"] 
# GBP, JPY, EUR 
USD = ["0.60","102.20","0.73"] 
# GBP, USD, EUR 
JPY = ["0.0059","0.0098","0.0072"] 
# GBP, USD, JPY 
EUR = ["0.82","1.36","139.70"] 
# Inputs and outputs 
user_input = -1 
output = -1 

while True: 
    print("") 
    print("----------------------------------------------------") 
    print("Example: 14 GBP to USD") 
    print("") 
    user_input = input("How much would you like to convert? ") 
    user_input = user_input.split() 


    if user_input[1] == "GBP": 
     if user_input[3] == "USD": 
      output = user_input[3] * USD[0] 
      print(output) 
     elif user_input[3] == "JPY": 
      print("Filler text") 
     elif user_input[3] == "EUR": 
      print("Filler text") 

    elif user_input[1] == "USD": 
     if user_input[3] == "GBP": 
      print("Filler text") 
     elif user_input[3] == "JPY": 
      print("Filler text") 
     elif user_input[3] == "EUR": 
      print("Filler text") 

    elif user_input[1] == "JPY": 
     if user_input[3] == "GBP": 
      print("Filler text") 
     elif user_input[3] == "USD": 
      print("Filler text") 
     elif user_input[3] == "EUR": 
      print("Filler text") 

    elif user_input[1] == "EUR": 
     if user_input[3] == "GBP": 
      print("Filler text") 
     elif user_input[3] == "USD": 
      print("Filler text") 
     elif user_input[3] == "JPY": 
      print("Filler text") 


    else: 
     print("Please input it as something like '15 GBP to USD' remembering capitals!") 

這是我得到的錯誤:

Traceback (most recent call last): 
    File "C:\Users\Kieran\Desktop\converter.py", line 27, in <module> 
    output = user_input[3] * USD[0] 
TypeError: can't multiply sequence by non-int of type 'str' 

我順便說一下我關於Python 3.3.3。

非常感謝!

+0

你有什麼預期的輸入和輸出? – thegrinner

+1

爲什麼你使用字符串作爲數字值?使用浮點數或小數類型。 – geoffspear

+0

你爲什麼要在輸入中調用'split'?你期望用戶輸入什麼? – IanAuld

回答

1

我會建議使用字典而不是列表,讓您的生活更輕鬆:

# USD, JPY, EUR 
GBP = ["1.66","169.14","1.21"] 

currencies ={"GBP" : {"USD": 1.66, "JPY": 169.14, "EUR": 1.21}, "USD": {blah, blah}

然後轉換給定的輸入​​:

(start_cur, end_cur) = user_input[1], user_input[3] # for readabilities sake 
converted = user_input[0] * currencies[start_cur][end_cur] 

整個程序:

## Currency conversion calculator Mk.3 ## 

currencies = { 
    "GBP" : {"USD": 1.66, "JPY": 169.14, "EUR": 1.21}, 
    "USD" : {"GBP": 0.60, "JPY": 102.20, "EUR": 0.73}, 
    "JPY" : {"GBP": 0.0059, "USD": 0.0098, "EUR": 0.0072}, 
    "EUR" : {"GBP": 0.82, "USD": 1.36, "JPY": 139.70} 
       } 

while True: 
    print("") 
    print("----------------------------------------------------") 
    print("Example: 14 GBP to USD") 
    print("") 

    try: 
     user_input = input("How much would you like to convert? ").split() 
     (start_cur, end_cur) = user_input[1], user_input[3] # for readabilities sake 
     converted = float(user_input[0]) * currencies[start_cur][end_cur] 
     print(converted) 

    except ValueError: 
     print("Please input it as something like '15 GBP to USD' remembering capitals!") 
+0

謝謝。我不想過度複雜它,但我絕對會考慮這個 – user3143129

+0

這樣你就不需要任何if/else邏輯,這會變得混亂。例如,如果您必須添加5個或6個以上的貨幣類型。這將爲您節省一些頭痛 – IanAuld

+0

非常感謝!我可能會用這個,非常感謝! – user3143129

2

更改線路

output = user_input[3] * USD[0] 

output = float(user_input[0]) * float(USD[0]) 

注意

由於錯誤提示,你想用字符串can't multiply sequence by non-int of type 'str'乘以一個序列(這裏的字符串) 。 顯然,這裏有兩個問題

  1. 您正在索引錯誤的項目。從您的數據中可以清楚的看出,貨幣價值爲0指數
  2. 您需要將貨幣值和轉換因子都轉換爲浮動。另外,您需要重新寫你的轉換系數列表作爲花車的列表,而不是字符串
+0

出現另一個錯誤: Traceback(最近調用最後一個): 文件「C:\ Users \ Kieran \ Desktop \ converter.py 「,第27行,在 output = int(user_input [0])* int(USD [0]) ValueError:無效文字爲int()與基數10:'0.60' – user3143129

+0

@ user3143129:請重新閱讀我的答案。當我更新我的回答時,您發現我了 – Abhijit

1

你綁乘兩個串在一起

user_input[3] = "USD"

USD[0] = "0.60"名單

output = user_input[3] * USD[0] 

您可能是指:

output = float(user_input[0]) * float(USD[0]) 

順便說一句,你也許應該讓所有這些整數,而不是字符串

USD = ["0.60","102.20","0.73"]USD = [0.60,102.20,0.73]

+0

將float轉換爲整數時您將面臨錯誤。 '無效文字爲int()與基地10' – Abhijit

+0

只是看到它,因爲你評論:) – jramirez

+0

這工作,非常感謝! – user3143129

1

不能在蟒蛇繁殖字符串,並得到一個算術響應。您需要將字符串轉換爲整數。更改以下行:

USD = [0.60,102.20,0.73] 

output = float(user_input[0]) * USD[0]) 
+0

USD [0]也表示爲字符串。 – Abhijit

0

您試圖通過另一個字符串乘以一個字符串。

試試這個:

output = float(user_input[0]) * float(USD[0]) 

USER_INPUT [3]是「英鎊」的字符串,我認爲你想說0.60