我正在使用random.random()随机浮动(显然!).但我真正想做的是:
there's a 30% chance my app does this:
pass
else:
pass
Run Code Online (Sandbox Code Playgroud)
你能帮帮我解决这个问题吗?
当我试图找出我的BeautifulSoup网络刮刀的低价和高价时,我收到此错误.我附上了以下代码.我的列表不应该是一个整数列表吗?
我在发布之前经历了类似的NoneType问题,但解决方案没有用(或者我可能不理解它们!)
Traceback (most recent call last):
File "/home/user-machine/Desktop/cl_phones/main.py", line 47, in <module>
print "Low: $" + intprices[0]
TypeError: 'NoneType' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)
相关代码段:
intprices = []
newprices = prices[:]
total = 0
for k in newprices:
total += int(k)
intprices.append(int(k))
avg = total/len(newprices)
intprices = intprices.sort()
print "Average: $" + str(avg)
print "Low: $" + intprices[0]
print "High: $" + intprices[-1]
Run Code Online (Sandbox Code Playgroud) 我一直在试验PythonAnywhere试图让一些python在Web服务器上工作.我最初从Arvixe切换,因为他们运行2.4并且PythonAnywhere的名字太吸引人了.
我的应用程序包含两个文件:phones.py和phonesearch.py.他们一起应该为电话价格刮取craigslist.
我在2.7本地测试,它运行得很好,生成一个带有表格和所有价格的html页面(celly.html).当我上传它时,它生成html就好了,但拒绝在我的价格列表中添加任何内容([intprices]).
我的怀疑:(a)因为它在本地工作正常,PythonAnywhere不允许它与craigslist通信; 或者(b)因为我这样做就像一个穴居人而不是使用微框架,PythonAnywhere否认我; 或(c)我对自己的错误视而不见,而且我错过了一些明显的错误.
我的Python脚本位于在/ home/tseymour/mysite的和正在生成的HTML 一样 /mysite/static/celly.html.该文件在http://tseymour.pythonanywhere.com/static/celly.html上提供
您会注意到我的所有单元格都填充了"N/A",这意味着它在try:"在SearchPhone.py中引发了一个IndexError.这意味着我的列表正在被填充!
但为什么会这样?!我相信这是因为我是PythonAnywhere n00b.
请指教.
SearchPhone.py
from BeautifulSoup import BeautifulSoup
import urllib
import re
def SearchPhone(phone):
y = "http://losangeles.craigslist.org/search/moa?query=" + phone + "+-%22buy%22+-%22fix%22+-%22unlock%22+-%22broken%22+-%22cracked%22+-%22parts%22&srchType=T&minAsk=&maxAsk="
site = urllib.urlopen(y)
html = site.read()
site.close()
soup = BeautifulSoup(html)
prices = soup.findAll("span", {"class":"itempp"})
prices = [str(j).strip('<span class="itempp"> $</span>') for j in prices]
for k in prices[:]:
if k == '': #left price blank
prices.remove(k)
elif int(k) <= 75: #less than $50: probably a service (or not …Run Code Online (Sandbox Code Playgroud) 我正在尝试解决未解决的外部问题(link2019 错误)。StackOverflow 上有很多关于这个问题的帖子,但要么我不理解这个错误,要么我对它视而不见。
该错误是由我的generate_maze函数引起的(特别是由rand_neighbor()调用引起的,对吧?)但我的理解是这些都已“解决”。
我稍微截断了代码,因为它非常冗长。我希望这是合适的。
void generate_maze (Vector<int> &coords, Grid<bool> &included, Maze &m);
int main() {
Grid<bool> included = initialize_grid();
Vector <int> coords = rand_coords();
Vector <int> current_point = coords;
generate_maze(coords, included, m);
return 0;
}
void generate_maze (Vector<int> &coords, Grid<bool> &included, Maze &m) {
while (gridIsTrue == false) {
Vector<int> neighbor = rand_neighbor(coords, included);
pointT neighborpoint = {neighbor[0], neighbor[1]};
pointT current_point = {coords[0], coords[1]};
if (included.get(neighbor[0], neighbor[1]) == false) {m.setWall(current_point, neighborpoint, false); included.set(neighbor[0], neighbor[1], true); current_point = neighborpoint;}
} …Run Code Online (Sandbox Code Playgroud) 我正在用Python设计一个库存类,它应该跟踪商店库存商品,添加新商品和删除它们.
问题来自我在课堂上的"项目"定义.当我将另一个项目添加到我的字典中时,它会替换它,它不会添加它.我感谢您的帮助!为什么不加?
class Store:
def __init__(self, name, email):
self.name = name
self.email = email
# two accessor methods
def getName(self):
return self.name
def getEmail(self):
return self.email
# makes print work correctly
def __str__(self):
return str(self.name)
# items
def additem(self, item, price):
global items
items = {}
self.item = str(item)
self.price = float(price)
items[self.item] = price
def delitem(self, item):
items.remove(item)
def displayinventory(self):
return items
Run Code Online (Sandbox Code Playgroud) 我正在为我的Python课程编写信用卡付款计算器.分配是为计算器编写一个定义,计算出每月支付的费用,以便在x个月内将余额归零.
该定义有3个参数:initialBalance,apr,months.
尽管我可以想象,分配的目的是让我们使用二分法来找到答案,并且我写了另外两个有助于赋值的定义:
1)newBalance() - 确定付款后的新余额; 2)余额() - 在付款后返回余额清单;
在这种情况下,balances()[ - 1]返回最终余额,所以我的行动计划是将列表中的最后一项评估为等于0(或至少在0的0.005之内!)如果是,则返回付款让我在那里.
如果最终余额是负数(我付了太多钱!):付款=付款 - (付款/ 2); 如果余额是正数(我没有足够的支付!):付款=付款+(付款/ 2);
尽我所能,我的算法应该最终得出结论,但它永远找不到足够接近的答案......
这是我的代码,(以及教授在最后的测试def):
def newBalance(prevBalance, apr, payment):
"""
- prevBalance: the balance on the credit card statement.
- apr: the annual percentage rate (15.9 here means 15.9%).
- payment: the amount paid this month to the credit card company.
- returns: the new balance that will be owed on the credit card
(assumes no purchases are made).
"""
interestCharge = float(((apr / 12.0) …Run Code Online (Sandbox Code Playgroud)