带有正方形的骰子游戏

时间:2018-07-17 13:25:23

标签: 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.randint(1,6)
        square()
      elif letter != 'YES':
        return("dommage")
    inputPlayerLetter()

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

这时我很受困扰,现在真的不需要更改或添加什么,如果有人有想法的话,那就太好了。

3 个答案:

答案 0 :(得分:1)

您的平方函数缺少参数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 ('|_____|')

向我输出以下输出

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

确保已定义函数inputPlayerLetter

答案 1 :(得分:0)

您的square函数需要参数x,但是在roll中,您调用square()时没有参数。您应该将x传递给它,然后从x = random.randint(1,6)函数中删除多余的square

答案 2 :(得分:0)

我对您的代码进行了一些重组。如果玩家输入no或以nN开头的任何单词,游戏将停止,否则将打印新的数字。我们只使用一个简单的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