我一直在学习Python,但我有点困惑.当我试图提高到一定数量时,在线教师告诉我使用操作符**而不是^.例:
print 8^3
Run Code Online (Sandbox Code Playgroud)
给出了11的输出.但我所寻找的(我被告知)更类似于:打印8**3给出512的正确答案.但为什么呢?
谁可以给我解释一下这个?为什么8 ^ 3不等于512,因为它是正确的答案?在什么情况下11(8 ^ 3的结果)?
我确实试图搜索SO,但我只看到有关在分割时获得模数的信息.
有一些代码给我带来了麻烦.它在我的另一个剧本中工作得很好,但我必须以某种方式弄乱它.
的if csv:主要是因为我是依托于一个argparser一个-csv选项.但即使我在外面使用适当的缩进来运行if statement它,它仍然会返回相同的错误.
import csv
if csv:
with open('output.csv', 'wb') as csvfile:
csvout = csv.writer(csvfile, delimiter=',',
quotechar=',', quoting=csv.QUOTE_MINIMAL)
csvout.writerow(['A', 'B', 'C'])
csvfile.close()
Run Code Online (Sandbox Code Playgroud)
给我:
Traceback (most recent call last):
File "import csv.py", line 34, in <module>
csvout = csv.writer(csvfile, delimiter=',',
AttributeError: 'str' object has no attribute 'writer'
Run Code Online (Sandbox Code Playgroud)
如果我删除了if statement,我得到:
Traceback (most recent call last):
File "C:\import csv.py", line 34, in <module>
csvout = csv.writer(csvfile, delimiter=',',
AttributeError: 'NoneType' object has no attribute 'writer'
Run Code Online (Sandbox Code Playgroud)
我做错了什么傻事?我确实尝试将文件名更改为test.py之类的东西,因为我在另一个SO帖子中看到了,但是没有用.
我正在学习Python,而且我遇到了一些问题.在我正在考虑的课程中看到类似的东西后,想出了这个简短的剧本.我之前用过"或"用"if"成功(这里没有显示太多).出于某种原因,我似乎无法让这个工作:
test = raw_input("It's the flying circus! Cool animals but which is the best?")
x = test.lower()
if x == "monkey" or "monkeys":
print "You're right, they are awesome!!"
elif x != "monkey" or "monkeys":
print "I'm sorry, you're incorrect.", x[0].upper() + x[1:], "is not the right animal."
Run Code Online (Sandbox Code Playgroud)
但是效果很好:
test = raw_input("It's the flying circus! Cool animals but which is the best?")
x = test.lower()
if x == "monkey":
print "You're right, they are awesome!!"
elif x != "monkey":
print …Run Code Online (Sandbox Code Playgroud)