我刚买了一本关于用Python编写圣诞节的孩子的书。我很高兴使用它并立即下载应用程序。该书说使用此代码导入Turtle:
//function to get the user's choice
const getUserChoice = userInput => {
userInput =
userInput.toLowerCase();
//if stmt to make sure input is valid
if (userInput === 'rock' || 'scissors' || 'paper') {
return userInput;
} else {
console.log('Invalid selection');
}//end else
}//end getUserChoice function
//function to get computer choice
const getComputerChoice = () => {
Math.floor(Math.random() * 3);
//switch case to verify & return result
switch (getComputerChoice) {
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
default:
console.log('Invalid');
break;
}//end switch
}//end getComputerChoice
//function to determine the winner
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'The game is a tie';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') { return 'You Won!'; }
} // end userchoice is rock
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'The computer won!';
} else {
return 'You won!';
}
} // end userchoice is paper
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!';
}
} //end userchoice is scissors
}//end winner function
//function to play the game
const playGame = () => {
const userChoice = getUserChoice('rock');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
//call funtion to determine winner
console.log(determineWinner(userChoice, computerChoice));
}//end playGame
//function calls
playGame();
然后它说要创建一个画布:
>>> import turtle
出现此错误消息。
>>> t = turtle.Pen ()
这是什么意思?我如何获得Turtle的画布?
答案 0 :(得分:0)
使用
# load the turtle module
import turtle
# the imported module turtle has a Turtle() that moves around
# give your turtle a name so you can refer to turtle.Turtle() by the shorter name
# tim (i.e.)
tim = turtle.Turtle()
# Move tim
tim.forward(50)
# end turtling
turtle.done()
tim
(turtle.Turtle()
)可以执行更多操作,此页面包含有关海龟,其方法和一些示例的信息:https://docs.python.org/3/library/turtle.html
答案 1 :(得分:0)
导入乌龟之后,你想输入一个变量名,即通过该变量调用乌龟。
语法:
import turtle
your_variable_name = turtle.Turtle()
之后,你想进入乌龟。
your_variable_name.pencolor('blue')
答案 2 :(得分:0)
你所做的事实上正确,尽管有评论和其他答案:
>>> import turtle
>>> t = turtle.Pen ()
例如:
> python3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
>>> t = turtle.Pen()
>>> t
<turtle.Turtle object at 0x101ca4ac8>
>>> ^D
问题是,为什么你不起作用?我最初的猜测是你在你的工作目录上创建了一个名为turtle.py
的文件 - 如果这样重命名或删除它并再试一次。否则,当您import turtle
时,Python会找到您的turtle.py文件而不是Python库turtle.py文件。如果情况并非如此:
我的第二个猜测是你正在使用一个有限的Python环境来实现它自己的乌龟子集。例如,https://trinket.io/turtle上使用乌龟的在线编码使用Pen()
无法工作的子集,但Turtle()
(它在标准海龟库中的别名)工作得很好。因此,请尝试Turtle()
而不是Pen()