相关疑难解决方法(0)

在if语句条件中使用"或"

我确信这有一个非常简单的答案,但在搜索了一段时间后我找不到它.

prefixes = "JKLMNOPQ"
suffix = "ack"

for letter in prefixes:
    if letter == "Q" or letter == "O":
        print letter + "u" + suffix
    else:
        print letter + suffix
Run Code Online (Sandbox Code Playgroud)

上面的代码工作得很好,if语句的条件似乎有点啰嗦,所以我试过:

if letter == "Q" or "O":
Run Code Online (Sandbox Code Playgroud)

这是较短但不起作用.我发现它不起作用,因为"O"是一个布尔表达式,它总是为True,并且它不考虑"或"左侧的任何内容.

我尝试将括号括在它周围:

if letter == ("Q" or "O"):
Run Code Online (Sandbox Code Playgroud)

但那只匹配Q而不是O.

有没有简单的方法让代码工作,或者我必须使用对我有用的冗长方式?

python

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

检查变量的多个值

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = str(word)[0]
    print first
    if str(first) == "a" or "e" or "i" or "u" or "o":
        print "vowel"
else:
    print "consonant"
Run Code Online (Sandbox Code Playgroud)

我想检查一个单词是否以元音或辅音开头.但是,这部分不起作用:如果str(first) == "a" or "e" or "i" or "u" or "o"

那么你如何检查第一个字母是"a"还是"e"或"i"或"u"还是"o"?

python variables

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

在Python中检测元音与辅音

我在这里犯了什么愚蠢的错误,这使我无法确定用户输入的第一个字母是辅音?无论我输入什么,它都允许评估第一个字母是元音.

original = raw_input('Enter a word:')
word = original.lower()
first = word[0]

if len(original) > 0 and original.isalpha():
    if first == "a" or "e" or "i" or "o" or "u":
        print "vowel"
    else:
        print "consonant"
else:
    print "empty"
Run Code Online (Sandbox Code Playgroud)

python string conditional conditional-statements

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

如何循环一个函数以每 15 分钟执行一次任务(在 0/15/30/45 分钟标记上)?

我有一个在 Raspberry Pi 上运行的程序,我想每 15 分钟在整点的 0、15、30 和 45 分钟从温度计中提取一些数据。

我已经使用 while 循环尝试过此操作,之前我有效地使用了 time.sleep(900),但有时会偏离整点后的 0、15、30 和 45 分钟。

目前我有这个;

from datetime import datetime

def run(condition):
    while condition == True:
        if (datetime.now().minute == (0 or 15 or 30 or 45)):
            #perform some task
        temperature_store()
Run Code Online (Sandbox Code Playgroud)

为简单起见,我没有深入了解Temperature_store() 的作用,但它从插入 Pi 的传感器读取温度,然后将其打印出来。

我希望Temperature_store() 每 15 分钟发生一次,但目前,它每秒发生一次。

我知道这可能是因为我的 while 循环的逻辑/语法错误,但我无法弄清楚。(对python脚本没有太多经验,耽误时间)。

python time while-loop python-datetime raspberry-pi

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

如何在 Dart 中同时比较 if 语句中的多个值

我有多个变量需要在 Dart 的 if 语句中进行相同的检查。我需要知道是否至少有一个变量 > 0。

例如:

var a = 1;
var b = 0;
var c = 0;

if ((a > 0) || (b > 0) || (c > 0)) {
  print('Yeh!');
}
Run Code Online (Sandbox Code Playgroud)

这应该更容易完成,就像在Python中一样。

以下代码无效,但我尝试过:

if ((a || b || c) > 0) {
  print('Yeh!');
}
Run Code Online (Sandbox Code Playgroud)

任何提示都会很好。

dart

5
推荐指数
1
解决办法
2900
查看次数

python复合和/或条件

if tarX == 'ini' or 'txt' and act == 'm':
Run Code Online (Sandbox Code Playgroud)

在普通英语中,if语句需要验证2个条件是否为真:

  1. tarX(变量)是ini或txt
  2. 该行为(变量)等于m

我尝试了几种变化无济于事.我确定这是我的语法/逻辑错误而不是python无能.下面的语句起作用是因为(假设)和语句左边的条件比较一个变量(tarX)表示单个字符串(exe)的相等性,同样适用于右变量(act)上的条件(与)相比(米).我假设我没有正确"打包"if tarX =='ini'或'txt'和act =='m':声明.

如果tarX =='exe'并且act =='m':

python if-statement conditional-operator conditional-statements

4
推荐指数
1
解决办法
9316
查看次数

[mccabe] 循环复杂度过高错误是什么意思?

我正在 repl.it 中为一个学校项目创建一个 CYOA,并且需要一个列表,所以我所做的就是在每次输入时为用户添加一个选项来查看他们的项目。在主要的分割处我放了

Items = ["Flask of Root Beer"]
print ("Intro:\nYou think to yourself as a 24yo dude with no life ahead of him 'Why am i stuck working in the urban district when I could be in the forest district exploring or something'. With the volcano looming over you, you head out the next morning leaving everything behind you and pursuing your dream of exploration. ")
print ()
print ("You depart from your house with your flask of …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

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

print("string1" 或 "string2" in string) 不给出布尔结果

为什么print("Lorem" and "aliqua" in string ) 给予真实。一个布尔值,

print("Lorem" or "aliqua" in string )给出了“Lorem”。一个字符串

string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"

print("Lorem" and "aliqua" in string )
>>> True

print("Lorem" or "aliqua" in string )
>>> Lorem
Run Code Online (Sandbox Code Playgroud)

python python-3.x

4
推荐指数
1
解决办法
422
查看次数

如果statement是false但是代码在其中,仍然和python中的整个脚本中的所有代码一样运行 - 对Python来说是新的

这是通过询问用户他们想要了解哪个元素来查找元素详细信息的代码.问题是当我运行它时会打印所有的print语句.

print ('Please type the element number or the name - no caps')
element = input('What element do you want to learn about?')


if element == ('1') or ('hydrogen'):
        print ('Hydrogen #1')
        print ('Gas')
        print ('Non-Metal')
        print ('Weight: 1.008')


if element == ('2') or ('helium'):
    print ('Helium #2')
    print ('Gas')
    print ('Non-Metal')
    print ('Weight: 4.0026')

if element == ('3') or ('lithium'):
    print ('Helium #3')
    print ('Solid')
    print ('Metal')
    print ('Weight: 6.94')
Run Code Online (Sandbox Code Playgroud)

这是我运行时发生的事情.

Please type the element number or the name …
Run Code Online (Sandbox Code Playgroud)

python if-statement python-3.x

3
推荐指数
1
解决办法
42
查看次数

嵌套"和/或"if语句

我正在处理创建列表的代码,然后应用"或"和"和"条件来执行进一步操作:

a= ["john", "carlos", "22", "70"]

if (("qjohn" or "carlos") in a) and (("272" or "70") in a):
    print "true"
else:
    print "not true"
Run Code Online (Sandbox Code Playgroud)

输出:

not true
Run Code Online (Sandbox Code Playgroud)

当我这样做:

a= ["john", "carlos", "22", "70"]

if ("qjohn" or "cdarlos" in a) and ("272" or "d70" in a):
    print "true"
else:
    print "not true"
Run Code Online (Sandbox Code Playgroud)

输出是 "true"

我没有得到的**carlos and 70**应该是真实的,但它打印"不真实".这个错误的原因是什么?谢谢

python boolean-logic list nested-if

3
推荐指数
1
解决办法
2844
查看次数