我正在尝试创建一个像吃角子老虎机一样工作的程序。在我的循环中,我试图根据一个玩家的旋转结果来决定他们获得多少学分。然而,假设一个玩家掷骰并赢得300个信用,当被要求再次旋转时,如果他们又赢了一次,它就不记得前一次旋转的结果。在
我知道这是一个简单的修复,但基本上每次循环返回时,它都会忘记更新后的总数。我对编程还不熟悉,这些东西对我来说简直就是地狱。在
下面是循环部分的内容:
cashPool = 500
import random
# Assigning wheel properties
wheel1 = ['Candy','Ghost','Pumpkin','Cat','Zombie','Witch','Witch','Cat','Ghost','Candy']
wheel2 = ['Candy','Ghost','Pumpkin','Cat','Zombie','Witch','Cat','Pumpkin','Ghost','Candy']
wheel3 = ['Candy','Ghost','Pumpkin','Cat','Zombie','Witch','Pumpkin','Candy','Candy','Ghost']
#loop to spin
def AskYesNo(question):
tmp = input(question).lower()
if tmp.startswith('y'):
return True
else:
return False
i = 0
while True:
spin1 = random.randint(3,4)
spin2 = random.randint(3,4)
spin3 = random.randint(3,4)
print(str(wheel1[spin1])+ '\t' + str(wheel2[spin2]+ '\t' + str(wheel3[spin3])))
i += 1
if spin1==4 and spin2==4 and spin3==4:
print('Congratulations! You have won 300 credits')
result = cashPool + 300
print('You currently have '+ str(result) +' credits')
if spin1==5 and spin2==5 and spin3==5:
print('Congratulations! You have won 250 credits')
result = cashPool + 250
print('You currently have '+ str(result)+ ' credits')
if spin1==3 and spin2==3 and spin3==3:
print('Congratulations! You have won 200 credits')
result = cashPool + 200
print('You currently have '+cashPool+' credits')
if spin1==2 and spin2==2 and spin3==2:
print('Congratulations! You have won 150 credits')
result = cashPool + 150
print('You currently have '+cashPool+' credits')
if spin1==1 and spin2==1 and spin3==1:
print('Congratulations! You have won 100 credits')
result = cashPool + 100
print('You currently have '+cashPool+' credits')
if spin1==0 and spin2==0 and spin3==0:
print('Congratulations! You have won 50 credits')
result = cashPool + 50
print('You currently have '+cashPool+' credits')
AskYesNo('Would you like to spin? Enter y/n ')
if False:
break
而且,我百分之百地肯定有一种更简单的方法来计算每次旋转的结果,而不是一遍又一遍地做if语句。但我又是新来的,这超出了我的能力。我不能像程序员一样思考,我不知道人们是怎么做到的。在
不要定义新的
result
变量,只需增加现有的cashPool
变量。E、 g.:首先,
random.randint(3,4)
只返回3或4。。。如果需要其余值,则需要为(0,4)
。在其次,这个部分不起作用,因为
if False
从来没有输入过。在你需要从那个函数中得到布尔值。在
^{pr2}$其他一些建议包括不要重复太多。使它DRY
例如,从
Python: determine if all items of a list are the same item
您可以提取一些变量常量,使用列表和一些函数编程来检查插槽。在
这样,你只需要玩数字,你就有了一个完全不同的游戏,只需要做最小的改变。在
注意:我知道我没有包括
wheel
值。当您获得wheel
列表中每个spin
值的字符串后,all_same
方法仍然有效。在相关问题 更多 >
编程相关推荐