在我正在阅读Python的书中,它继续使用代码 eval(input('blah'))
我阅读了文档,我理解它,但我仍然没有看到它如何改变input()
功能.
它有什么作用?谁能解释一下?
可以说我有以下代码:
import collections
d = collections.OrderedDict()
d['foo'] = 'python'
d['bar'] = 'spam'
Run Code Online (Sandbox Code Playgroud)
有没有办法以编号的方式访问这些项目,例如:
d(0) #foo's Output
d(1) #bar's Output
Run Code Online (Sandbox Code Playgroud) 在python中,说是否有区别:
if text == 'sometext':
print(text)
if text == 'nottext':
print("notanytext")
Run Code Online (Sandbox Code Playgroud)
和
if text == 'sometext':
print(text)
elif text == 'nottext':
print("notanytext")
Run Code Online (Sandbox Code Playgroud)
只是想知道多个if
s是否会导致任何不必要的问题,以及是否更好的做法使用elif
s.
我正在学习Ruby,并且已经达到了令我感到困惑的程度.
我使用的这本书是在谈论private
,public
和protected methods
,但我还是有点困惑.每个之间有什么区别?
我正在尝试使用数组,并正在阅读Steve Holzner撰写的"Begin on Ruby on Rails"一书.我做了这个程序:
array = ['Hello', 'there', 1, 2]
puts array[1]
puts array[3]
puts array.length
array2 = Array.new
puts array2.length
array2[0] = "Banana"
array2[1] = 6
puts array2[0] + " " + array2[1]
puts array3.length
Run Code Online (Sandbox Code Playgroud)
它没有做太多,但是当我运行它时,我得到了错误
arrays.rb:9:in `+': can't convert Fixnum into String (TypeError)
from arrays.rb:9
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此错误?
所以我从我用来学习Python的书中复制并粘贴了一个演示程序:
#!/usr/bin/env python
import csv
total = 0
priciest = ('',0,0,0)
r = csv.reader(open('purchases.csv'))
for row in r:
cost = float(row[1]) * float(row[2])
total += cost
if cost == priciest[3]:
priciest = row + [cost]
print("You spent", total)
print("Your priciest purchase was", priciest[1], priciest[0], "at a total cost of", priciest[3])
Run Code Online (Sandbox Code Playgroud)
我得到错误:
Traceback (most recent call last):
File "purchases.py", line 2, in <module>
import csv
File "/Users/Solomon/Desktop/Python/csv.py", line 5, in <module>
r = csv.read(open('purchases.csv'))
AttributeError: 'module' object has no attribute 'read' …
Run Code Online (Sandbox Code Playgroud) 我正在用Python编程,我想知道我是否可以测试我的代码中是否调用了函数
def example():
pass
example()
#Pseudocode:
if example.has_been_called:
print("foo bar")
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
假设我想用用户输入的代码创建一个文档,称为spam.txt
.假设我有用户输入,请说:
input("Is Python Good? ")
Run Code Online (Sandbox Code Playgroud)
如何将用户输入的文本保存到文本文件中,并且可以执行此操作?
我想找到关于使用curl的网页的信息,但在Python中,到目前为止我有这个:
os.system("curl --head www.google.com")
Run Code Online (Sandbox Code Playgroud)
如果我运行它,它会打印出来:
HTTP/1.1 200 OK
Date: Sun, 15 Apr 2012 00:50:13 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=3e39ad65c9fa03f3:FF=0:TM=1334451013:LM=1334451013:S=IyFnmKZh0Ck4xfJ4; expires=Tue, 15-Apr-2014 00:50:13 GMT; path=/; domain=.google.com
Set-Cookie: NID=58=Giz8e5-6p4cDNmx9j9QLwCbqhRksc907LDDO6WYeeV-hRbugTLTLvyjswf6Vk1xd6FPAGi8VOPaJVXm14TBm-0Seu1_331zS6gPHfFp4u4rRkXtSR9Un0hg-smEqByZO; expires=Mon, 15-Oct-2012 00:50:13 GMT; path=/; domain=.google.com; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked
Run Code Online (Sandbox Code Playgroud)
我想要做的是,能够使用正则表达式匹配其中的200(我不需要帮助),但是,我找不到将上述所有文本转换为字符串的方法.我怎么做?我试过了,info = os.system("curl --head www.google.com")
但info
只是0
.
让我们说你有一套:
foo = {1, 2, 3, 4, 5}
Run Code Online (Sandbox Code Playgroud)
在我正在阅读的书中,Pro Python,它说使用foo.pop()
将弹出该选择中的任意数字.但是......当我尝试它时,它pops 1, then 2, then 3...
是否随意做,或者这只是巧合?
python ×8
python-3.x ×2
ruby ×2
algorithm ×1
arrays ×1
collections ×1
csv ×1
dictionary ×1
eval ×1
hashtable ×1
if-statement ×1
save ×1
set ×1
text ×1