我打字以获得销售额(按输入)乘以定义的销售税(0.08),然后打印总金额(销售税时间销售金额).
我遇到了这个错误.谁知道什么可能是错的或有任何建议?
salesAmount = raw_input (["Insert sale amount here \n"])
['Insert sale amount here \n']20.99
>>> salesTax = 0.08
>>> totalAmount = salesAmount * salesTax
Traceback (most recent call last):
File "<pyshell#57>", line 1, in <module>
totalAmount = salesAmount * salesTax
TypeError: can't multiply sequence by non-int of type 'float'
Run Code Online (Sandbox Code Playgroud) 现在我将vector3值表示为列表.有没有办法减去其中2个像vector3值,比如
[2,2,2] - [1,1,1] = [1,1,1]
Run Code Online (Sandbox Code Playgroud)
我应该使用元组吗?
如果它们都没有在这些类型上定义这些操作数,我可以改为定义它吗?
如果没有,我应该创建一个新的vector3类吗?
如何将参数绑定到Python方法以存储一个用于以后调用的nullary仿函数?与C++类似boost::bind.
例如:
def add(x, y):
return x + y
add_5 = magic_function(add, 5)
assert add_5(3) == 8
Run Code Online (Sandbox Code Playgroud) 码:
import urllib2 as u
import os as o
inn = 'dword.txt'
w = open(inn)
z = w.readline()
b = w.readline()
c = w.readline()
x = w.readline()
m = w.readline()
def Dict(Let, Mod):
global str
inn = 'dword.txt'
den = 'definitions.txt'
print 'reading definitions...'
dell =open(den, 'w')
print 'getting source code...'
f = u.urlopen('http://dictionary.reference.com/browse/' + Let)
a = f.read(800)
print 'writing source code to file...'
f = open("dic1.txt", "w")
f.write(a)
f.close()
j = open('defs.txt', 'w')
print 'finding definition is source …Run Code Online (Sandbox Code Playgroud) 我想做这样的事情:
myList = [10,20,30]
yourList = myList.append (40)
Run Code Online (Sandbox Code Playgroud)
不幸的是,list append不会返回修改后的列表.
那么,我如何允许append返回新列表?
我正在尝试运行此代码,我有一个列表列表.我需要添加到内部列表,但我得到错误
TypeError: 'list' object is not callable.
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我这里我做错了什么.
def createlists():
global maxchar
global minchar
global worddict
global wordlists
for i in range(minchar, maxchar + 1):
wordlists.insert(i, list())
#add data to list now
for words in worddict.keys():
print words
print wordlists(len(words)) # <--- Error here.
(wordlists(len(words))).append(words) # <-- Error here too
print "adding word " + words + " at " + str(wordlists(len(words)))
print wordlists(5)
Run Code Online (Sandbox Code Playgroud) 如何将函数应用于变量输入列表?例如,filter函数返回true值,但不返回函数的实际输出.
from string import upper
mylis=['this is test', 'another test']
filter(upper, mylis)
['this is test', 'another test']
Run Code Online (Sandbox Code Playgroud)
预期的产出是:
['THIS IS TEST', 'ANOTHER TEST']
Run Code Online (Sandbox Code Playgroud)
我知道upper是内置的.这只是一个例子.
我有一个非常简单的python脚本,它应该扫描一个文本文件,其中包含格式为id =' value '的行并将它们放入一个dict中.python模块名为chval.py,输入文件名为in.txt.这是代码:
import os,sys
from os import *
from sys import *
vals = {}
f = open(sys.argv[1], 'r')
for line in val_f:
t = line.split('=')
t[1].strip('\'')
vals.append(t[0], t[1])
print vals
f.close()
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我得到:
回溯(最近一次调用最后一次):
文件"chval.py",第9行,在?f = open(sys.argv [1],'r')TypeError:需要一个整数
我正在使用python 2.4 ...因为我一直被要求不使用任何更新的东西,是否有一些我不知道的open()?为什么要整数?
该行之后的任何内容都是未经测试的.简而言之:为什么它会给我错误,我该如何解决?
我有一个Python脚本:
if True:
if False:
print('foo')
print('bar')
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行我的脚本时,Python提出了一个IndentationError:
File "script.py", line 4
print('bar')
^
IndentationError: unindent does not match any outer indentation level
Run Code Online (Sandbox Code Playgroud)
我一直在玩我的程序,我还能够产生其他三个错误:
IndentationError: unexpected indentIndentationError: expected an indented blockTabError: inconsistent use of tabs and spaces in indentation这些错误意味着什么?我究竟做错了什么?我该如何修复我的代码?
我无法弄清楚我在使用Python 2.7编写的代码时遇到的问题.我正在将引用转换为int,但我一直在获得类型异常bad operand type for unary +: 'str'.有人可以帮忙吗?
import urllib2
import time
import datetime
stocksToPull = 'EBAY', 'AAPL'
def pullData(stock):
try:
print 'Currently pulling', stock
print str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/' + \
stock + '/chartdata;type=quote;range=3y/csv'
saveFileLine = stock + '.txt'
try:
readExistingData = open(saveFileLine, 'r').read()
splitExisting = readExistingData.split('\n')
mostRecentLine = splitExisting[-2]
lastUnix = mostRecentLine.split(',')[0]
except Exception, e:
print str(e)
time.sleep(1)
lastUnix = 0
saveFile = open(saveFileLine, 'a')
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for eachLine in splitSource: …Run Code Online (Sandbox Code Playgroud)