2016-04-03 110 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

相关问题