2016-04-03 107 views
1

所以我正在研究一個小的輪盤程序,當我嘗試添加/添加到用戶餘額時,它會出現錯誤已被標題,是什麼讓程序做到這一點,我該如何解決它?Python - 不支持的操作數類型爲 - :'int'和'str'

import random 
import time 

balance = 100 

outcome = random.randint(0,17) 
if outcome == 0: 
    color = "GREEN" 
elif outcome <=7: 
    color = "RED" 
else: 
    color = "BLACK" 

print("Current Balance: $"+str(balance)) 
colorChoice = input("Place your bet by typing either: RED, GREEN or BLACK\n") 
colorChoice = colorChoice.upper() 
betAmount = input("How much would you like to bet?\n") 
if int(betAmount) > balance: 
    print("Insufficient Funds") 
else: 
    print("** ROLLING **") 
    time.sleep(2.5) 
    print("The color landed on: " + color) 
    if colorChoice == color and color == "GREEN": 
     print("Win! Your balance has been adjusted!\nYou selected: " +  colorChoice + " and the spinner landed on: " + color) 
     greenLand = betAmount * 14 
     balance = balance + greenLand 
    elif colorChoice == color and color == "RED": 
     print("Win! Your balance has been adjusted!\nYou selected: " + colorChoice + " and the spinner landed on: " + color) 
     balance = balance + betAmount 
    elif colorChoice == color and color == "BLACK": 
     print("Win! Your balance has been adjusted!\nYou selected: " + colorChoice + " and the spinner landed on: " + color) 
     balance = balance + betAmount 
    elif colorChoice != color and color == "GREEN": 
     print("Loss! Your balance has been adjusted!\nYou selected: " + colorChoice + " and the spinner landed on: " + color) 
     balance = balance - betAmount   
    elif colorChoice != color and color == "RED": 
     print("Loss! Your balance has been adjusted!\nYou selected: " + colorChoice + " and the spinner landed on: " + color) 
     balance = balance - betAmount   
    elif colorChoice != color and color == "BLACK": 
     print("Loss! Your balance has been adjusted!\nYou selected: " + colorChoice + " and the spinner landed on: " + color) 
     balance = balance - betAmount   
    print("New Balance: $" + str(balance)) 

只是讓這個帖子並沒有得到下投票,我想再次重申,當我試圖操縱在底部的if/elif的語句平衡可變我的問題是發生!

+1

您的'betAmount'變量是一個字符串,而您的'balance'是一個整數。顯然,你不能從一個字符串中減去一個整數。將'betAmount'轉換爲一個整數,就像你在'int(betAmount)> balance'中做的那樣。請注意,int()調用不會在代碼中的任何地方改變它們的參數類型,只是在你調用它的地方 –

回答

4

您的betAmount應聲明爲int(input("How much would you like to bet?\n"))以使其成爲整數。並如評論,使整數與整數匹配。算術。

+0

這很有效,非常感謝。 – Senoj

相關問題