可能重复:
Python如何比较字符串和int?
我有一个Python脚本没有按预期评估两个值.该值'10'被确定为大于200.问题是保持'10'值的变量实际上是一个字符串而不是整数(而是200一个整数).
我的问题是:
在针对整数计算字符串时,Python经历了什么过程?它是如何进行比较的?
例如:
string="10"
int=200
if string >= int:
print("String is greater")
else:
print("Int is greater")
Run Code Online (Sandbox Code Playgroud)
输出:
String is greater
Run Code Online (Sandbox Code Playgroud)
为什么是这样?我本以为Python在尝试比较两种类型时会退出并出错.
可能重复:
Python如何比较字符串和int?
为什么下面的部分表现得像它的表现?
>>> '10' > 100
True
>>> 100 < '10'
True
Run Code Online (Sandbox Code Playgroud)
它不应该引发例外吗?
我的代码遇到了一些问题.如果值不在0-255之间,我希望我的代码弹出一个消息框,但它不起作用.我现在只是使用c> 255进行故障排除,但我不知道问题似乎是什么.即使它的c> 255,它仍然会在值低于255时显示消息框.有人能告诉我我做错了什么:\
def clicked_wbbalance(self):
self.top = Toplevel()
self.top.title("LASKJDF...")
Label(self.top, text="Enter low level").grid(row=0, column=0,padx=10)
Label(self.top, text="Enter high level").grid(row=1, column=0,padx=10)
Label(self.top, text="Values must be between 0 to 255").grid(row=3, column=0)
self.a = Entry(self.top)
self.b = Entry(self.top)
self.a.grid(row=0, column=1,padx=10)
self.b.grid(row=1, column=1,padx=10)
Button(self.top, text="Ok", command=self.get).grid(row=3, column = 1)
def get(self):
self.c = self.a.get()
self.d = self.b.get()
if self.c > 255:
tkMessageBox.showerror("Error", "Please enter a value between 0-255")
self.clicked_wbbalance()
else:
print "asdfasd"
Run Code Online (Sandbox Code Playgroud) 这是教程的一部分.我的目标是从用户那里获取输入,然后在条件为真时递增它.
inputNum = raw_input("What is max number?")
def incrementF(greatest):
i = 0
numbers = []
while i < greatest:
print "At the top of the list numbers is %d" % i
numbers.append(i)
i += 1
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers:"
for num in numbers:
print num
incrementF(inputNum)
Run Code Online (Sandbox Code Playgroud)
当我运行脚本并为inputNum输入任何内容而不是几行输出时,我似乎遇到了无限循环.
例如,原始输入提示是10,我希望看到类似的东西:
Run Code Online (Sandbox Code Playgroud)At the top of the list numbers is 0, Numbers now: 0, 0 At the top of the list …
a = raw_input ("enter a number")
i = 0
numbers = []
while i < a:
print "At the top i is %d" % i
numbers.append(i)
i = i + 1
print "Numbers now:", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num
Run Code Online (Sandbox Code Playgroud)
所以我正在关注lpthw并且只是搞乱了代码,为什么当我使用raw_input并输入一个像6这样的数字时这个循环会变成无限循环?不应该i = i + 1在那里阻止这种情况发生吗?
我试图编写一个简单的程序:
import random
x = raw_input("How many rounds:")
rounds = 0
while rounds < x:
# rock=0, paper=1, scissors=2
computer1 = random.randint(0,2)
computer2 = random.randint(0,2)
if computer1 == computer2:
print "draw"
elif computer1 == 0 and computer2 == 1:
print "lose"
elif computer1 == 1 and computer2 == 2:
print "lose"
elif computer1 == 2 and computer2 == 0:
print "lose"
else:
print "win"
rounds = rounds + 1
Run Code Online (Sandbox Code Playgroud)
为什么这会给我一个无限循环?当我取出输入行并将x替换为某个值(例如10)时,输出会给出10个结果.但为什么我不能用raw_input做呢?
我有一个如下所示的列表:
a = ['e8:04:62:23:57:d0\t2462\t-55\t[WPA2-PSK-CCMP][ESS]\tTest', '00:1b:2f:48:8b:f2\t2462\t-57\t[WPA2-PSK-CCMP-preauth][ESS]\tT_test', 'e8:04:62:23:4e:70\t2437\t-61\t[WPA2-PSK-CCMP][ESS]\tT_guest', 'e8:04:62:23:57:d1\t2462\t-53\t[ESS]\t', 'e8:04:62:23:4e:71\t2437\t-56\t[ESS]\t']
Run Code Online (Sandbox Code Playgroud)
我想根据每个元素的第三个选项卡后的数字对列表进行排序,因此在这种情况下,-55、-57、-61、-53 应将列表顺序更改为 -53、-55、-57、-61。我尝试过的任何方式似乎都非常复杂(制作列表等)。我应该使用正则表达式/模式来简化吗?
当我尝试max()在包含整数和字符串组合的列表上调用函数时,它总是返回一个字符串,在Python 2.x中
例如,
#input
li = [2, 232322, 78, 'python', 77]
max(li)
#output
'python'
Run Code Online (Sandbox Code Playgroud)
这背后的逻辑似乎是什么?Python如何将字符串与整数进行比较?
我有一个程序在运行时解析输出,比较从给定值的时间和打印差异,但它不按我的方式工作:
a = 'Time Taken for Response: 31 msec'
time_out = 75
if a.split()[4] > time_out:
print "time taken is more than given conditions"
print a.split()[4]
Run Code Online (Sandbox Code Playgroud)
输出如下:
time taken is more than given conditions
31
Run Code Online (Sandbox Code Playgroud)
我不明白为什么程序进入循环本身时 31 < 75
任何线索或指导???
我定义了以下函数来测试输入是否为正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.谢谢!
python ×10
python-2.7 ×3
string ×3
list ×2
comparison ×1
evaluation ×1
function ×1
integer ×1
max ×1
numbers ×1
python-2.x ×1
return ×1
sorting ×1
tkinter ×1
variables ×1
while-loop ×1