嘿,每个人我都想做一个骰子平方,作为一个初学者,在我尝试围绕要返回的数字创建平方之前,效果还不错,
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 ('|_____|')
这时我很受困扰,现在真的不需要更改或添加什么,如果有人有想法的话,那就太好了。
答案 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
或以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