Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 巨蟒骰子游戏指南_Python_Function - Fatal编程技术网

Python 巨蟒骰子游戏指南

Python 巨蟒骰子游戏指南,python,function,Python,Function,在我的Python类中,我们现在正在做远程项目,最近的一个项目让我感到困惑。我们将创建一个骰子游戏,其功能如下: 1.输入您的下注金额 2.然后掷骰子,结果设为“点” 3.再次掷骰子,7或11导致赢,2、3或12导致输,任何其他结果导致重新掷骰 我可以写这段代码,但我们已经得到了一个框架脚本,必须使我们的解决方案适合它。由于我在函数方面的经验有限,我很难做到这一点。我曾试着自学,但这本骨架剧本真的让我很反感。我非常感谢您的帮助,如果这个问题在这里不合适,我深表歉意。我不是在找人帮我写解决方案,只

在我的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
请输入您的赌注:1
回溯(最近一次呼叫最后一次):
文件“D:/School/CSCI 256/Project 2/p1_2.py”,第43行,在
main()
文件“D:/School/CSCI 256/Project 2/p1_2.py”,第37行,主视图
结果,点=设定点(下注)
文件“D:/School/CSCI 256/Project 2/p1_2.py”,第13行,在设定点中
点=die1+die2
NameError:未定义名称“die1”
进程已完成,退出代码为1

您没有将
rollTheDice
的返回值赋给变量,因此解释器不知道
die1
die2
是什么。这些变量的名称是函数本身的局部变量,因此您也必须在
设定点
函数中定义它们。如果将其更改为以下内容:

def setPoint(bet):  # returns the outome of the game and the point
    die1, die2 = rollTheDice()
    point = die1 + die2
    print('You rolled a ' + point + '.')
    print('This is the new point.')
    return point, outcome
它应该知道这些变量。但是,
结果
变量仍然存在问题,该变量也未定义


您返回的点和结果变量的顺序似乎也与预期的不同,因此请注意。

我应该提到,我已经尝试让这件事工作了12个多小时,因此我尝试,但我会从中发现错误,你知道吗,因为我不太熟悉用这种方式使用函数。你到底有什么问题?你是在写enterBet、rollTheDice等吗?@Sheldon我想我的第一个困惑点是:为什么setPoint()不把die1和die2加起来,求和来找到点?我在最初设置点时遇到问题。在我看来,这个想法可能是setPoint函数调用rollTheDice函数,并使用返回值来设置点。@FlamFace我尝试过,但在调用它之后,我似乎无法在setPoint()中求和,因为它说这些返回值没有定义。如果需要的话,我可以用错误来发布我的最佳尝试代码。非常感谢,我认为这是我做错了,但不知道如何正确地做。一点也不担心。习惯变量的范围,也就是解释程序知道变量的地方,可能会很棘手。