新的python /编程.我试图通过在给定字符串中插入一个字符来创建每个可能单词的列表.
例如
'thx' = ['athx','tahx','thax','thxa']
Run Code Online (Sandbox Code Playgroud)
我可以通过用if/else分割我的循环来完成这个,但是我试图在没有它的情况下解决 - 我似乎无法找到一种方法来将字符添加到开头和结尾.(athx和thxa)
从这看起来,似乎唯一的方法是使用正则表达式.但是,我还没有.真的只是想确保我不会在更基础的层面上遗漏任何东西.
我想知道如何在 Python 的列表中只取正值。
例如,如果我有A = [1, 2, 3],它应该返回[1, 2, 3]
如果我有A = [-1, 2, 3],它应该返回[2, 3]
如果我有A = [-1, -2],它应该返回None
非常感谢!
我有一个用python编程的discord机器人。我希望机器人说笑话的第一部分,即time.sleep,然后讲笑话的第二部分(都在同一个变量中)。这是我的代码:
if message.content.startswith('!joke'):
a = 'Can a kangaroo jump higher than a house?' + time.sleep(3) + 'Of course, a house doesn’t jump at all.'
b = 'Anton, do you think I’m a bad mother?' + time.sleep(3) + 'My name is Paul.'
c = 'Why can\'t cats work with a computer?' + time.sleep(3) + 'Because they get too distracted chasing the mouse around, haha!'
d = 'My dog used to chase people on a bike a lot.' + time.sleep(3) + 'It …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,如何在'end'函数中为变量'猜测'提供.每当我尝试这个,我只是收到猜测没有定义.在播放函数中,我返回一个数字,如果我理解正确,那么该数字应该等于'猜猜',并且由于某种原因我不理解'猜测'将不会在'结束'中起作用.
def main():
guesses = play()
play_again = again()
while (play_again == True):
guesses = play()
play_again = again()
total_games = 1
total_games += 1
end()
def end():
print("Results: ")
print("Total: " + print(str(guesses + 1)))
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试在 python 中编写一个代码,如果您对它的回复在可能的答案列表中,它将回复您。程序示例如下。
def responce():
greetings = ["Hello","Hi","Nice to see you!","Greetings","How's it going?","How are you doing?","What's new?","How's your day going?","Hey!"]
print("\n")
reply = input(": ")
lenrep = len(reply)
tempstore = []
for i in range(0,lenrep):
tempstore.append(i)
z = 0
while z < 9:
tempgreet = greetings[z]
if tempstore.upper() == tempgreet.upper():
reply = ""
tempstore = []
temprandno = random.randint(0,2)
addon = ["what do you want to know?", "what do you want to talk about?", " "]
addontext = addon[temprandno]
text(greet(), …Run Code Online (Sandbox Code Playgroud) 我正在开发一个程序,它将获取列表并返回一个列表,其中包含相邻相同值的嵌套列表.例如,输入列表
[1,2,4,5,5,7,6,6,6]
Run Code Online (Sandbox Code Playgroud)
会回来的
[1,2,4,[5,5],7,[6,6,6]]
Run Code Online (Sandbox Code Playgroud)
我将如何编写函数来执行此操作?