Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.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_Python 3.x - Fatal编程技术网

Python 骰子游戏,周围有正方形

Python 骰子游戏,周围有正方形,python,python-3.x,Python,Python 3.x,嘿,大家好,作为一个初学者,我正在试着做一个骰子方块,在我试着在返回的数字周围创建一个方块之前,这个方块非常好 import random def roll(): letter = '' while not (letter == 'YES'): print('Do you want to to roll?') letter = input().upper() if letter == 'YES': x = random.r

嘿,大家好,作为一个初学者,我正在试着做一个骰子方块,在我试着在返回的数字周围创建一个方块之前,这个方块非常好

import random

def roll():

    letter = ''
    while not (letter == 'YES'):
      print('Do you want to to roll?')
      letter = input().upper()

      if letter == 'YES':
        x = random.randint(1,6)
        square()
      elif letter != 'YES':
        return("dommage")
    inputPlayerLetter()

def square(x):
 x = random.randint(1,6)
 print (' _____')
 print ('|     |')
 print ('|', x , '|')
 print ('|_____|')

我几乎被困在这一点上,现在真的不知道要更改或添加什么,如果有人有主意的话,那就太好了。

您的
square
函数需要一个参数
x
,而在
roll
中,您可以无参数调用
square()
。您应该将
x
传递给它,并从
square
函数中删除冗余的
x=random.randint(1,6)

您的square函数缺少参数
x

import random

def roll():

    letter = ''
    while not (letter == 'YES'):
        print('Do you want to to roll?')
        letter = input().upper()

    # the first element in the tuple is the player's letter, the second is the computer's letter.
    if letter == 'YES':
        x = random.randint(1,6)
        square(x)
    elif letter != 'YES':
        return("dommage")
    inputPlayerLetter()

def square(x):
    x = random.randint(1,6)
    print (' _____')
    print ('|     |')
    print ('|', x , '|')
    print ('|_____|')
import random

def square(x):
    print(' _____')
    print('|     |')
    print('| ', x, ' |')
    print('|_____|')

def roll():
    while True:
        letter = input('Do you want to roll? [Y/n] ').lower()
        if letter.startswith('n'):
            print("Dommage")
            break
        else:
            square(random.randint(1, 6))

roll()    
将以下输出打印给我

Do you want to to roll?
yes
 _____
|     |
| 4 |
|_____|

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-58-26b32eade6f0> in <module>()
----> 1 roll()

<ipython-input-57-5376e02fea33> in roll()
     14     elif letter != 'YES':
     15         return("dommage")
---> 16     inputPlayerLetter()
     17 
     18 def square(x):

NameError: name 'inputPlayerLetter' is not defined
是否要滚动?
对
_____
|     |
| 4 |
|_____|
---------------------------------------------------------------------------
NameError回溯(最近一次呼叫上次)
在()
---->1卷()
滚动
14 elif字母!='是的:
15返回(“dommage”)
--->16 InputPlayerLitter()
17
18 def方形(x):
名称错误:未定义名称“InputPlayerLitter”

确保已经定义了函数
InputPlayerLitter

我重新组织了您的代码。如果玩家输入
no
,或任何以
n
n
开头的单词,游戏将停止,否则将打印一个新的数字。我们只是使用一个简单的
while
循环来进行循环。这比调用
roll
函数本身要好。我还修复了
square
函数中的间距错误

import random

def square(x):
    print(' _____')
    print('|     |')
    print('| ', x, ' |')
    print('|_____|')

def roll():
    while True:
        letter = input('Do you want to roll? [Y/n] ').lower()
        if letter.startswith('n'):
            print("Dommage")
            break
        else:
            square(random.randint(1, 6))

roll()    
典型输出

Do you want to roll? [Y/n] Yes
 _____
|     |
|  5  |
|_____|
Do you want to roll? [Y/n] y
 _____
|     |
|  6  |
|_____|
Do you want to roll? [Y/n] maybe
 _____
|     |
|  4  |
|_____|
Do you want to roll? [Y/n] no
Dommage

你想修复的代码有什么问题?@khelwood我想在数字(1,6)周围画一个正方形,看起来像骰子的正面。@khelwood说你想解决什么问题,因为有很多问题。另外,确保包含所有相关代码(Aka.
inputPlayerLetter()
)。@Basho不,这正是您要做的。您需要修复的代码有什么问题?它会产生错误吗?它是否产生了错误的输出?您有这样的评论:
#元组中的第一个元素是播放器的字母,第二个元素是计算机的字母。
但是我在代码中看不到任何元组。是的,我很糟糕,我忘记更改名称,因为发布的inputPlayer确实是roll()谢谢!我很难理解何时以及在函数参数中输入什么!