我正在努力做的就是讓「現金」變成一種平衡。因此我希望能夠改變這個價值。但是當我運行程序時,變量「現金」的價值似乎沒有改變,當我從它減去。如果玩家在擲骰子上錯誤地猜測,該玩家應該丟掉現金。更多信息在代碼本身。如果答案有一個簡短的解釋,我會非常感激,我會這樣做來學習,它的純粹建設性。PYTHON:「賭博」計劃。無法更改變量的值? (v。2.7)
print ""
import time
cash = 5000
print 'you brought','$',cash,'today'
while cash>0:
from random import randint
die = randint(1,1)
while True:
try:
print
choice1 = int(raw_input('First guess: '))
print
choice2 = int(raw_input('Second guess: '))
print
break
except ValueError:
print 'Please, enter a number.'
print 'rolling die..'
time.sleep(3)
if choice1+choice2==die:
#PROBLEM: The operation below does not change the value of cash, why not?.
cash=cash+1000
print cash
print 'you rolled',die
print 'win! you won $1000, you\'re new balance is:',cash
#PROBLEM: The new val of cash should be printed here^but it remains as 5000
else:
cash-1000
print 'you rolled',die
print 'lose! you lost $1000, you\'re new balance is:',cash
if cash<0:
print 'Bankrupt.'
time.sleep(3)
quit()
if cash==1000000:
print 'Millionaire!'
break
我猜這只是一個簡單的錯字,OP要我們爲他調試,而不是自己做,而不是他不知道如何分配變量。如果我猜錯了,那麼你的答案比甲酯的解釋更好...... – abarnert