相关疑难解决方法(0)

如果python中的字符串语句?

我是一个初学者,一直在看http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements,但我无法理解这里的问题.这很简单,如果用户输入y它应该打印这将进行计算虽然我在IF回答= ="y"时出现语法错误

answer = str(input("Is the information correct? Enter Y for yes or N for no"))
proceed="y" or "Y" 
If answer==proceed:
print("this will do the calculation"):
else:
exit()
Run Code Online (Sandbox Code Playgroud)

python conditional-statements

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

将字符串与Python中的多个项目进行比较

我正在尝试将一个被调用的字符串facility与多个可能的字符串进行比较来测试它是否有效.有效字符串是:

auth, authpriv, daemon, cron, ftp, lpr, kern, mail, news, syslog, user, uucp, local0, ... , local7
Run Code Online (Sandbox Code Playgroud)

有没有一种有效的方法来做到这一点:

if facility == "auth" or facility == "authpriv" ...
Run Code Online (Sandbox Code Playgroud)

python string string-comparison

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

使用OR与IF语句进行比较

在Python中使用IF语句时,必须执行以下操作才能使"级联"正常工作.

if job == "mechanic" or job == "tech":
        print "awesome"
elif job == "tool" or job == "rock":
        print "dolt"
Run Code Online (Sandbox Code Playgroud)

有没有办法让Python在检查"等于"时接受多个值?例如,

if job == "mechanic" or "tech":
    print "awesome"
elif job == "tool" or "rock":
    print "dolt"
Run Code Online (Sandbox Code Playgroud)

python comparison boolean

13
推荐指数
1
解决办法
6万
查看次数

在String Python中计算元音

我正在尝试计算字符串中特定字符的出现次数,但输出错误.

这是我的代码:

inputString = str(input("Please type a sentence: "))
a = "a"
A = "A"
e = "e"
E = "E"
i = "i"
I = "I"
o = "o"
O = "O"
u = "u"
U = "U"
acount = 0
ecount = 0
icount = 0
ocount = 0
ucount = 0

if A or a in stri :
     acount = acount + 1

if E or e in stri :
     ecount = ecount + 1

if I …
Run Code Online (Sandbox Code Playgroud)

python

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

if/elif语句的多个条件

我试图从多个条件触发if语句,而不用不同的触发器多次重写语句.例如:

if user_input == "look":  
    print description


if user_input == "look around":
    print description
Run Code Online (Sandbox Code Playgroud)

你会如何将它们浓缩成一个陈述?

我尝试使用'或',它会导致任何raw_input触发语句,无论输入是否匹配任何条件.

if user_input == "look" or "look around":  
    print description
Run Code Online (Sandbox Code Playgroud)

python if-statement python-2.7

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

"或"有条件的Python麻烦

我正在学习Python,而且我遇到了一些问题.在我正在考虑的课程中看到类似的东西后,想出了这个简短的剧本.我之前用过"或"用"if"成功(这里没有显示太多).出于某种原因,我似乎无法让这个工作:

test = raw_input("It's the flying circus! Cool animals but which is the best?")
x = test.lower()

if x == "monkey" or "monkeys":
    print "You're right, they are awesome!!"
elif x != "monkey" or "monkeys":
    print "I'm sorry, you're incorrect.", x[0].upper() + x[1:], "is not the right animal."
Run Code Online (Sandbox Code Playgroud)

但是效果很好:

test = raw_input("It's the flying circus! Cool animals but which is the best?")
x = test.lower()

if x == "monkey":
    print "You're right, they are awesome!!"
elif x != "monkey":
    print …
Run Code Online (Sandbox Code Playgroud)

python conditional-operator conditional-statements

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

如果L中的'a'或'b',其中L是一个列表(Python)

我遇到以下逻辑问题:

让我们说我有一个清单 L = ['a', 'b', 'c']


这两个项目都在列表中......

if ('a' or 'b') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
Run Code Online (Sandbox Code Playgroud)

版画 It's there!


只有第一个项目在列表中...

if ('a' or 'd') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
Run Code Online (Sandbox Code Playgroud)

版画 It's there!


列表中没有任何项目......

if ('e' or 'd') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
Run Code Online (Sandbox Code Playgroud)

版画 No sorry


这是令人困惑的一个只有列表中的第二项......

if ('e' or 'a') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
Run Code Online (Sandbox Code Playgroud)

版画 No sorry


我不明白为什么这不是一个真实的陈述.这如何概括为具有n个 …

python

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

Python中的vs ==.在这种情况下使用哪种?

我正在进行AJAX调用并pub在其中传递变量,这可能是10.

作为一个初学者,我想要对进入的变量类型进行双重确认.我知道我可以很容易地转换为int()问题实际上不是AJAX结果,但它导致了这个问题.

我的代码:

if pub == 1 or pub == '1':
    #execute funcA()
Run Code Online (Sandbox Code Playgroud)

但上面对我来说不是那么pythonic所以我试过:

if pub in [1,'1']:
    #execute funcA()
Run Code Online (Sandbox Code Playgroud)

以上哪个代码在以下方面更好:

  1. 性能(速度).
  2. 最佳实践.
  3. 内存使用情况.

python

7
推荐指数
2
解决办法
1549
查看次数

在Python中一起使用IF,AND,OR和EQUAL操作数

我正在尝试创建一个函数,其中检查给定值(作为字符串传递)以查看数字位数是4还是6,并且它是一个数字.

我的第一个冲动是使用这段代码:

def number(x):
    if (len(x) == (4 or 6)) and x.isdigit():
        print "True"
    else:
        print "False"
Run Code Online (Sandbox Code Playgroud)

上面的代码只通过了下面的第一个测试......我不明白为什么它通过了这个,但没有其他测试:

number("1234")
Run Code Online (Sandbox Code Playgroud)

只有当我分离出len()函数时才能正常工作.

def number(x):
    if (len(x) == 4 or len(x) == 6) and x.isdigit():
        print "True"
    else:
        print "False"


## Checks
number("1234")
number("123456")
number("abcd")
number("abcdef")
number("1")
number("a")
Run Code Online (Sandbox Code Playgroud)

上面的代码通过了所有测试.

所以我的问题是:

  1. 这里发生了什么?
  2. 有没有办法为此编写更干净的代码?

谢谢你的帮助!

**这不是一个重复的问题,因为虽然这个问题与布尔运算符有相同的基本概念,但由于len(),isdigit()的使用以及如何最好地改进它的另一个问题(有人评论),问题本身是不同的返回的用法).但是,肯定会为另一个问题添加另一个视角.

python conditional boolean operand

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

如何将 str.startswith 与多个字符串一起使用?

我尝试使用 or 函数为同一输出输入多个单词,但它只将第一个单词作为输入,而不将其余单词作为输入。我该如何解决这个问题?谢谢!例如:

message.content.startswith("hi" or "hey")
Run Code Online (Sandbox Code Playgroud)

只接受“hi”作为输入,而不接受“hey”。

我尝试过将单词添加到列表中,但效果不佳。我对编码比较陌生,所以如果这是一个愚蠢的问题,我提前表示抱歉

python discord

6
推荐指数
1
解决办法
5298
查看次数