我正在阅读python函数并看到这段代码:
def happyBirthday(person):
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear " + person + ".")
print("Happy Birthday to you!")
happyBirthday('Emily')
happyBirthday('Andre')
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么这些括号被用于打印命令,所以我删除了它们.
def happyBirthday(person):
print "Happy Birthday to you!"
print "Happy Birthday to you!"
print "Happy Birthday, dear " + person + "."
print "Happy Birthday to you!")
happyBirthday('Emily')
happyBirthday('Andre')
Run Code Online (Sandbox Code Playgroud)
即使在删除这些括号后,我得到了完全相同的结果,所以我不确定哪一个是正确的,或者我是否应该使用这些括号.是否真的有必要使用这些括号?
还有一件事.+person+亲爱的安德烈,当我使用括号然后将结果作为生日快乐.但是当我使用 ,person,它时,结果为"生日快乐,亲爱的","安德烈",".">
我无法理解结果中的这些差异.你能否对此有所了解?
我正在按照"如何像计算机科学家那样思考"来学习python,并且在理解类和对象章节时遇到了一些问题.
那里的一个练习是写一个名为moveRect的函数,它接受一个Rectangle和两个名为dx&dy的参数.它应该通过将dx添加到角的x坐标和dy到角的y坐标来改变矩形的位置.
现在,我不确定我写的代码是否正确.那么,让我告诉你我想做什么,你可以告诉我,我做得对吗?
首先我创建了一个类Rectangle然后我创建了一个实例并输入了细节,例如坐标x和y的值以及矩形的宽度和高度.
所以,这是我之前的代码:
class Rectangle:
pass
rect=Rectangle()
rect.x=3.0
rect.y=4.0
rect.width=50
rect.height=120
def moveRect(Rectangle,dx,dy):
Rectangle.x=Rectangle.x + dx
Rectangle.y=Rectangle.y + dy
dx=raw_input("enter dx value:")
dy=raw_input("enter dy value:")
moveRect(Rectangle,dx,dy)
Run Code Online (Sandbox Code Playgroud)
但是当我运行这段代码时,它给了我一个属性错误,并且:类Rectangle没有属性x
因此,我将以下行移动到moveRect函数中
rect=Rectangle()
rect.x=3.0
rect.y=4.0
rect.width=50
rect.height=120
Run Code Online (Sandbox Code Playgroud)
因此代码成了:
class Rectangle:
pass
def moveRect(Rectangle,dx,dy):
Rectangle.x=Rectangle.x + dx
Rectangle.y=Rectangle.y + dy
rect=Rectangle()
rect.x=3.0
rect.y=4.0
rect.width=50
rect.height=120
dx=raw_input("enter dx value:")
dy=raw_input("enter dy value:")
moveRect(Rectangle,dx,dy)
Run Code Online (Sandbox Code Playgroud)
但是,这段代码仍然给我一个错误.那么,这段代码实际上有什么问题呢?目前,我觉得好像我使用反复试验编写了这段代码,并在看到错误时改变了部分.我想正确理解这是如何运作的.所以,请详细说明一下.
这本书"如何像计算机科学家一样思考"并未在第12章中介绍init,因此我需要在不使用init的情况下完成.
我正在学习Python中的字典,我创建了一个简单的程序:
# Create an empty dictionary called d1
d1 = {}
# Print dictionary and length
def dixnary():
print "Dictionary contents : "
print d1
print "Length = ", len(d1)
# Add items to dictionary
d1["to"] = "two"
d1["for"] = "four"
print "Dictionary contents :"
print d1
print "Length =" , len(d1)
# Print dictionary and length
print dixnary()
Run Code Online (Sandbox Code Playgroud)
现在,当我使用print命令和使用该dixnary函数时,结果会有所不同.
使用print命令我得到结果:
字典内容:
<'to':'two','for:'four'>
Length = 2
当我使用该功能时dixnary,我得到了结果:
字典内容:
<'到':'two','for:'four'>
长度= 2
无 …
我正在创建一个简单的 react redux 应用程序,我有 json 格式的数据,即 characters.json
问题是,当我启动应用程序时,我在 JSON 中的位置 6 处得到了一个 Unexpected token i
你能说出这里有什么问题吗?
[
{
id: 0,
name: "Superman",
strength: 10,
intelligence: 7,
speed: 9
},
{
id: 1,
name: "Batman",
strength: 7,
intelligence: 10,
speed: 6
},
{
id: 2,
name: "Wonderwoman",
strength: 5,
intelligence: 9,
speed: 7
},
{
id: 3,
name: "Flash",
strength: 5,
intelligence: 6,
speed: 10
},
{
id: 4,
name: "Green Lantern",
strength: 7,
intelligence: 8,
speed: 7
},
{
id: …Run Code Online (Sandbox Code Playgroud) 在下面的代码中理解2件事时遇到问题.问题1:我不明白图是什么,它的用途是什么.通常当我们用这里的2个参数创建函数时:
def find_city(themap,state):我们不应该进入的2个参数的值
themap,并state当我们运行程序?然而,我们只给出状态值,即我们输入CA OR MI或FL.我不明白用的是什么themap.
问题2:我不明白cities['_find'] = find_city
我搜索谷歌的行'_find' python,我发现的唯一一件事是参考zed shaw的书.它应该属于哪个类别,或者我应该阅读哪些内容以了解有关此行的更多信息?
cities = {'CA': 'San Francisco', 'MI': 'Detroit',
'FL': 'Jacksonville'}
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
def find_city(themap, state):
if state in themap:
return themap[state]
else:
return "Not found."
# ok pay attention!
cities['_find'] = find_city
while True:
print "State? (ENTER to quit)",
state = raw_input("> ")
if not state: break
# this line is the most important ever! …Run Code Online (Sandbox Code Playgroud) 我想做的是从表单获取数据,但是表单是动态生成的,所以我不知道会有多少行。所以对于我拥有的表单(使用ejs)
<form action="/attendance-data" method="post">
<% var i=0 %>
<% rows.forEach(function(item){ %>
<% i=i+1 %>
<tr>
<td><%= i+"." %></td>
<td id="uan<%= i %>"> <%= item.uan %> </td>
<td><%= item.name %></td>
<td><%= item.designation %></td>
<td><input type="text" name="attendance<%= i %>" ></td>
</tr>
<% }); %>
<input type="submit" value="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
在这里,我从1更改为10或任何数据长度。因此,示例表单名称来自Attenance1,Attenance2,Attenance3,.....
在服务器端,我正在使用
app.post('/attendance-data',function(req,res){
var daysPresent;
var attendance;
for(var i=1;i<=numberOfEmployees;i++){
attendance = "attendance"+i;
daysPresent = req.body.attendance;
console.log('attendance is :',attendance);
console.log('days present is :',daysPresent);
}
});
Run Code Online (Sandbox Code Playgroud)
在这里,我想从表单中获取每个循环的当前天数。因此,第一个循环获取req.body.attendance1,第二个获取req.body.attendance2,依此类推,但是当我直接使用变量Attenance而不是Attenance1和Attenance2时,结果就变得不确定。
当我直接使用req.body.attendance1或req.body.attendance2或类似的东西时,我只会得到结果。
为什么会这样呢?为什么我不能使用出勤变量,该变量从Attenance1,Attenance2 ......获取所有值
我正在使用splice从数组中删除一个元素,但它不起作用.据我所知,代码看起来没问题,但也许我错过了/监督某些事情.请看一下.这里'state'是一个包含对象的数组.
let removeFromState = state;
for(var i=0;i<removeFromState.length;i++){
if(removeFromState[i].index===action.index){
removeFromState.splice[i,1];
}
}
return removeFromState;
Run Code Online (Sandbox Code Playgroud)
我不敢相信,我真是太傻了.我已经看了很长一段时间,但没有在我面前看到它.但我很高兴我在这里发布,因为关于缺少条目的评论,因为我正在增加'我',即使我正在删除条目.
有没有办法在定义函数时询问用户输入?例如,这是一个简单的功能:
def sum3(a,b,c):
add=a+b+c
return add
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,有没有办法可以让用户输入3个数字.比如说,当程序运行时,用户会看到提示"请输入3个数字"