相关疑难解决方法(0)

Python如果a = this或this或this

如果我有一个功能:

if a_string == "a" or a_string == "b" or a_string == "c":
    # do this
Run Code Online (Sandbox Code Playgroud)

如果没有重复或陈述,我怎么能以不同的方式写出来?或者这是最好的方式?

python

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

Python在IN和OR运算符中使用列表中的for循环

我对在python中执行此操作的正确方法感到困惑....所以如果我想使用for循环遍历列表并检查列表'A'的每个元素是否在2个或更多其他列表中的任何一个但是我似乎不明白怎么做...这里是我的意思的一些基本代码:

>>> a
[1, 2, 3, 4, 5]
>>> even
[2, 4]
>>> odd
[1, 3]
>>> for i in a:
...     if i in even or odd:
...             print(i)
... 
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)

为什么这个代码打印5因为5不在偶数列表中,也不在奇数列表中?另外,正确的方法是什么,以便我可以迭代一个列表并检查每个元素是否在ATLEAST中的其他一些列表中?

python for-loop boolean list in-operator

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

什么是做多个ors的pythonic方法?

有没有办法有效地做:

if operator != ('+' or '-' or '*' or '/'):
Run Code Online (Sandbox Code Playgroud)

无需做

operator != '+' and operator != '-' and operator != '*'
Run Code Online (Sandbox Code Playgroud)

python

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

在python中使用"不等于"的最佳方法是什么?

有什么更好的我可以使用/导入?

 while StrLevel != "low" or "medium" or "high":
        StrLevel = input("Please enter low, medium, or high for the program to work; ")
Run Code Online (Sandbox Code Playgroud)

python

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

if语句下的Python-Multiple条件

我正在尝试编写一个函数,该函数将对给定数据集中的商品进行分类(我知道这是非常简单的方式)。

看起来像:

def classifier(x):
    if ('smth' or 'smth' or 'smth') in x:
        return 'class1'
    elif ('smth' or 'smth' or 'smth') in x:
        return 'class2'
Run Code Online (Sandbox Code Playgroud)

因此,问题在于某些条件不起作用。当我尝试分别检查条件时-一切正常。但是在函数中出现了问题。

我将事物功能与pandas apply-method一起使用:

data['classes'] = data['subj'].apply(lambda x: classifier(x))
Run Code Online (Sandbox Code Playgroud)

python string if-statement

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

在两个选项之间随机选择

我对 python 很陌生,我正在努力更好地学习这门语言。我正在尝试创建一些非常简单的东西,我正在玩if语句,我正在尝试选择一个随机选项,并根据选择的内容有不同的响应。我的东西看起来像这样......

species = "cat" or "dog"
if species == "cat":
    print("yep, it's a cat")
else:
    print("nope, it's a dog")
Run Code Online (Sandbox Code Playgroud)

但结果总是猫。我如何让它随机选择?

python random if-statement python-2.7

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

逻辑或运算符的行为不符合预期

def Forest(Health,Hunger):
    print'You wake up in the middle of the forest'
    Inventory = 'Inventory: '
    Squirrel =  'Squirrel'
    while True:
        Choice1 = raw_input('You...\n')
        if Choice1 == 'Life' or 'life':
            print('Health: '+str(Health))
            print('Hunger: '+str(Hunger))
        elif Choice1 == 'Look' or 'look':
            print 'You see many trees, and what looks like an edible dead Squirrel, \na waterfall to the north and a village to the south.'
        elif Choice1 == 'Pickup' or 'pickup':
            p1 = raw_input('Pickup what?\n')
            if p1 == Squirrel:
                if Inventory == …
Run Code Online (Sandbox Code Playgroud)

python if-statement logical-or

0
推荐指数
1
解决办法
168
查看次数

为什么我的程序不会退出?

好吧,所以我已经尝试了一段时间了,一旦它运行,我似乎找不到关闭程序的方法.

我想要它做的是如果用户选择'no'以下结束程序:

elif y == 'n' or 'no':
    sys.exit(0)
Run Code Online (Sandbox Code Playgroud)

无论我选择什么,都会将程序返回给partOne函数.我尝试过不同的尝试来解决这个问题,比如搬家

partOne()
Run Code Online (Sandbox Code Playgroud)

从程序结束到两个函数之间但是不起作用,因为那时PartTwo函数尚未定义.

感谢您的答复.

import hashlib
import sys

def partOne():
    x = input("\nEnter something to hash: ")
    hash_object = hashlib.sha256(x.encode())
    hex_dig = hash_object.hexdigest()
    print("\nPlain text: ")
    print(x)
    print("\nHashed text: ")
    print(hex_dig)
    print("\nYour password: ")
    print(hex_dig[::9])
    partTwo()

def partTwo():
    y = input("\nDo you want to make another password? (y/n): ")
    if y == 'y' or 'yes':
        partOne()
    elif y == 'n' or 'no':
        sys.exit(0)
    else:
        print("\nYou …
Run Code Online (Sandbox Code Playgroud)

python function exit quit

0
推荐指数
1
解决办法
115
查看次数

多个Python if-statments?

我通常不用Python开发,但我有一些IDE和代码编辑器,我经常使用和切换.为了让自己更方便,我想我会制作一个快速的Python程序,根据输入启动我的IDE/Editor.问题是,每次运行程序时,第一个if-statment总是验证为true并运行该操作.

这是我的代码:

import os

#NOTE: I have trimmed the root directories here to save space. Just removed the subfolder names, but the programs are the same. 

notepadPlusPlusLaunch = "C:\\Notepad++\\notepad++.exe"
bracketsLaunch = "C:Brackets\\Brackets.exe"
aptanaLaunch = "C:Aptana Studio\\AptanaStudio3.exe"
devCppLaunch = "C:Dev-Cpp\\devcpp.exe"
githubLaunch = "C:GitHub,  Inc\\GitHub.appref-ms"
androidLaunch = "C:android-studio\\bin\\studio64.exe"
ijLaunch = "C:bin\\idea.exe"
pycharmLaunch = "C:JetBrains\\PyCharm  4.0.5\\bin\\pycharm.exe"
sublimeLaunch = "C:Sublime Text 3\\sublime_text.exe"

def launcherFunction(command):
    os.startfile(command)

launchCommand = input("")

if launchCommand == "notepad" or "npp" or "n++" or "n":
    launcherFunction(notepadPlusPlusLaunch)

elif launchCommand == "brackets" or …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

0
推荐指数
1
解决办法
156
查看次数

Python:检查字符串是否等于其他两个字符串的更短方法

我刚刚开始学习python这个问题出现在我的脑海中,是否有更短的方法来确定一个字符串是否等于'某事'或'somethingelse'?

例:

input = raw_input("question?")

while input != 'n' and input != 'y':
    #ask question again
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
117
查看次数