我正在用 python 编写一个非常基本的轮盘赌模拟器。目前,我只关注红/黑投注(基本上与投注正面或反面相同,使用硬币)。
我的代码有各种问题。请原谅我非常基本的语言知识。
import random
# Defines initial amounts of money and losses
money = 50
losses = 0
# Asks user how much to bet
def roulette_sim():
print "How much do you want to bet?"
bet = raw_input("> ")
if bet > money:
bet_too_much()
else:
red_or_black()
# Prevents one from betting more money than one has
def bet_too_much():
print "You do not have all that money. Please bet again."
raw_input("Press ENTER to continue> ")
roulette_sim()
# Asks user to select red or black, starts the sim, modifies money/losses
def red_or_black():
print "OK, you bet %r" % (bet)
print "Red or black?"
answer = raw_input("> ")
number = random.randint(1, 2)
if number == 1 and answer == "red":
print "You win!"
money += bet
print "You now have %r money" % (money)
print "Your losses are %r" % (losses)
replay()
elif number == 2 and answer == "black":
print "You win!"
money += bet
print "You now have %r money" % (money)
print "Your losses are %r" % (losses)
replay()
else:
print "You lost!"
money -= bet
losses += bet
print "You now have %r money" % (money)
print "Your losses are %r" % (losses)
replay()
# Asks user whether he/she wants to play again
def replay():
print "Do you want to play again?"
play_again = raw_input("y/n> ")
if play_again == "y":
roulette_sim()
else:
print "OK, bye loser!"
roulette_sim()
到目前为止的第一个问题:bet_too_much 函数不起作用。无论我输入多少,程序都会说它太多(即:“赌注”总是大于“金钱”)。为什么?
到目前为止的第二个问题:当我想通过使用例如:money += bet python 将“bet”添加到“money”/从中减去“bet”时,python 将其视为将 int 与字符串相加(至少我认为是这样)而不是将两个变量相加。为什么是这样?
任何帮助,将不胜感激。
谢谢