在我的Python类中,我们现在正在做远程项目,最近的一个项目让我感到困惑。我们将创建一个骰子游戏,其功能如下: 1.输入您的下注金额 2.然后掷骰子,结果设为“点” 3.再次掷骰子,7或11导致赢,2、3或12导致输,任何其他结果导致重新掷骰
我可以写这段代码,但我们已经得到了一个框架脚本,必须使我们的解决方案适合它。由于我在函数方面的经验有限,我很难做到这一点。我曾试着自学,但这本骨架剧本真的让我很反感。我非常感谢您的帮助,如果这个问题在这里不合适,我深表歉意。我不是在找人帮我写解决方案,只是一些指导。提前谢谢
以下是基本脚本:
import random
def enterBet(): # returns amount bet (1-1000)
def rollTheDice(): # returns die1, die2
def setPoint(bet): # returns the outome of the game and the point
def playDice(bet, point): # returns the outcome
OUTCOME_WIN = 1
OUTCOME_LOSE = 2
OUTCOME_REROLL = 3
def main():
bet = enterBet()
outcome, point = setPoint(bet)
while outcome == OUTCOME_REROLL:
outcome = playDice(bet,point)
if __name__ == "__main__":
main()
以下是我迄今为止最好的尝试:
import random
def enterBet(): # returns amount bet (1-1000)
int(input("Please Enter Your Bet: "))
def rollTheDice(): # returns die1, die2
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
return die1, die2
def setPoint(bet): # returns the outome of the game and the point
rollTheDice()
point = die1 + die2
print('You rolled a ' + point + '.')
print('This is the new point.')
return point, outcome
def playDice(bet, point): # returns the outcome
print('Point is: ' + point )
if point == 7 or point == 11:
outcome = OUTCOME_WIN
elif point == 2 or point == 3 or roll == 12:
outcome = OUTCOME_LOSE
else:
outcome = OUTCOME_REROLL
return outcome
OUTCOME_WIN = 1
OUTCOME_LOSE = 2
OUTCOME_REROLL = 3
def main():
bet = enterBet()
outcome, point = setPoint(bet)
while outcome == OUTCOME_REROLL:
outcome = playDice(bet, point)
if __name__ == "__main__":
main()
下面是我得到的错误:
Please Enter Your Bet: 1
Traceback (most recent call last):
File "D:/School/CSCI 256/Project 2/p1_2.py", line 43, in <module>
main()
File "D:/School/CSCI 256/Project 2/p1_2.py", line 37, in main
outcome, point = setPoint(bet)
File "D:/School/CSCI 256/Project 2/p1_2.py", line 13, in setPoint
point = die1 + die2
NameError: name 'die1' is not defined
Process finished with exit code 1
您没有将
rollTheDice
的返回值赋给变量,因此解释器不知道die1
和die2
是什么。这些变量的名称是函数本身的局部变量,因此您也必须在setPoint
函数中定义它们。如果将其更改为以下内容:它应该知道这些变量。但是,
outcome
变量仍然存在问题,该变量也没有定义您似乎返回的点和结果变量的顺序也与预期的不同,因此请注意这一点
相关问题 更多 >
编程相关推荐