2016-04-03 14 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'は整数です。明らかに、文字列から整数を引くことはできません。 'int(betAmount)> balance'のように、' betAmount'を整数に変換します。 'int()'の呼び出しでは、あなたのコード内の引数の型はどこでもあなたが呼び出された場所のどこでも変更されないことに注意してください。 –

答えて

4

betAmountは整数にするにはint(input("How much would you like to bet?\n"))と宣言してください。そしてコメントとして、整数を整数と一致させる。算術的に。

+0

これはうまくいきました。 – Senoj

関連する問題