相关疑难解决方法(0)

为什么我得到TypeError:不能将序列乘以'float'类型的非int?

我打字以获得销售额(按输入)乘以定义的销售税(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)

python

80
推荐指数
2
解决办法
28万
查看次数

在Python中减去2个列表

现在我将vector3值表示为列表.有没有办法减去其中2个像vector3值,比如

[2,2,2] - [1,1,1] = [1,1,1]
Run Code Online (Sandbox Code Playgroud)

我应该使用元组吗?

如果它们都没有在这些类型上定义这些操作数,我可以改为定义它吗?

如果没有,我应该创建一个新的vector3类吗?

python tuples list vector

75
推荐指数
8
解决办法
13万
查看次数

Python参数绑定器

如何将参数绑定到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)

python partial-application

60
推荐指数
5
解决办法
2万
查看次数

TypeError:'str'对象不可调用(Python)

码:

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)

python

59
推荐指数
10
解决办法
48万
查看次数

允许Python list append方法返回新列表

我想做这样的事情:

myList = [10,20,30]
yourList = myList.append (40)
Run Code Online (Sandbox Code Playgroud)

不幸的是,list append不会返回修改后的列表.

那么,我如何允许append返回新列表?

python list append

58
推荐指数
4
解决办法
4万
查看次数

TypeError:尝试访问列表时,'list'对象不可调用

我正在尝试运行此代码,我有一个列表列表.我需要添加到内部列表,但我得到错误

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)

python callable

52
推荐指数
4
解决办法
41万
查看次数

将函数应用于列表的每个元素

如何将函数应用于变量输入列表?例如,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 function list

43
推荐指数
3
解决办法
10万
查看次数

需要一个整数?打开()

我有一个非常简单的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 file-io integer argv

38
推荐指数
4
解决办法
8万
查看次数

我得到一个IndentationError.我如何解决它?

我有一个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 indent
  • IndentationError: expected an indented block
  • TabError: inconsistent use of tabs and spaces in indentation

这些错误意味着什么?我究竟做错了什么?我该如何修复我的代码?

python error-handling code-formatting exception indentation

38
推荐指数
1
解决办法
2万
查看次数

一元+的错误操作数类型:'str'

我无法弄清楚我在使用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)

python operands

27
推荐指数
2
解决办法
10万
查看次数