我试图打破for循环,但由于某种原因,以下不能按预期工作:
for out in dbOutPut:
case_id = out['case_id']
string = out['subject']
vectorspace = create_vector_space_model(case_id, string, tfidf_dict)
vectorspace_list.append(vectorspace)
case_id_list.append(case_id)
print len(case_id_list)
if len(case_id_list) >= kcount:
print "true"
break
Run Code Online (Sandbox Code Playgroud)
它只是一直在迭代,直到结束dbOutput.我究竟做错了什么?
我定义了以下函数来测试输入是否为正int.我打算使用eval(raw_input("...")),这就是为什么try-except部分在那里:
def is_ok(x): # checks if x is a positive integer
is_int = False
is_pos = False
try:
if type(eval(x)) == int:
is_int = True
if x > 0:
is_pos = True
else:
pass
else:
pass
except NameError:
is_int = False
print "not even a number!"
return is_int, is_pos
Run Code Online (Sandbox Code Playgroud)
如果我尝试传递一个正整数,它将返回True,如预期的那样.它返回False,False和非数字的错误消息.
但是,对于负数,它仍然返回True以检查它是否为正数.例如,在函数后添加:
is_int, is_pos = is_ok("-9")
print is_int, is_pos
Run Code Online (Sandbox Code Playgroud)
正在运行的打印:真实
无法理解为什么会这样,并且会爱你的帮助.即使有更有效的方法来实现这一点,我仍然想了解为什么它产生True True.谢谢!
temp = '32'
if temp > 85:
print "Hot"
elif temp > 62:
print "Comfortable"
else:
print "Cold"
Run Code Online (Sandbox Code Playgroud)
为什么输出'热',不应该是'冷'?
此代码通常所说的最小数量在列表前面在输入的名单,但出于某种原因,每当12和2被inputed像这样Please enter your cards(x, y, z,...): 12, 2它输出
12, 2
12, 2
Run Code Online (Sandbox Code Playgroud)
这是代码:
#!/usr/bin/python2.7
cards_input = raw_input("Please enter your cards(x, y, z, ...): ")
print cards_input
cards = cards_input.split(", ")
minimum = min(cards)
cards.remove(min(cards))
cards.insert(0, minimum)
print cards
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
maximum = max(1, 1.25, 3.14, 'a', 1000)- 为什么要给出'a'答案?不应该'a'转换为ASCII并进行检查?
maximum = max(1, 2.15, "hello")给出"hello"答案.这个答案是怎么来的?
我想在python中制作一个猜谜游戏.
from random import randint
print "\nI'm thinking of a number, you have to guess what it is.\n"
num = randint(1,100)
guess = 0
while guess != num:
guess = raw_input("Guess the number \n")
if guess < num:
print "Guess higher next time \n"
elif guess > num:
print "Guess lower next time \n"
elif guess == num:
print "That's correct \n"
Run Code Online (Sandbox Code Playgroud)
问题是无论我输入什么号码,我每次都会得到"猜数较低的数字".那有什么不对?