请看代码.我正在使用机器人汽车来画一个字母,在这个代码中,当我输入b时,它仍然会画一个小案例a.
import create
# Draw a:
def drawa():
#create robot
robot = create.Create(4)
#switch robot to full mode
robot.toFullMode()
for i in range(1280):
robot.go(20,30)
robot.stop()
robot.move(-40,20)
# Draw b:
def drawb():
#create robot
robot = create.Create(4)
#switch robot to full mode
robot.toFullMode()
robot.move(-100,20)
for i in range(1270):
robot.go(20,-30)
robot.stop()
# Draw c:
def drawc():
#create robot
robot = create.Create(4)
#switch robot to full mode
robot.toFullMode()
for i in range(700):
robot.go(20,30)
robot.stop()
# Define Main Function
def main():
# While loop …Run Code Online (Sandbox Code Playgroud) 使用python 2.7.5
x = (raw_input)'''What will you Reply?
a. Nod
b. Answer
c. Stare
'''))
if x == "Nod" or "a" or "nod":
print '''You nod shyly.
"Oh that's Great"'''
elif x == "Answer" or "b" or "answer":
print '''You answer in a low tone.
"You can speak!"'''
elif x == "Stare" or "c" or "stare":
print '''you stare blankly, so does the AI.
"Well okay then."'''
Run Code Online (Sandbox Code Playgroud)
当我运行它时,无论我在提示中输入什么,它只会触发'''你害羞地点头."哦,太好了"'''
但是,如果我复制它并将其粘贴到我的python shell中,它有一个"哦"的问题,如果我摆脱它,它有一个问题在"那是",如果我只是摆脱"这是伟大的"它对下一个elif语句的前三个字符有问题.WTF是错误的,我的python代码和shell最近工作正常,能够拆分if和elif.但现在突然间它只是不想.
我做了一些代码,它将滚动两个模具并询问用户多少次.之后,它将询问用户是否想再次播放.由于某种原因,它应该没有突破其中一个while循环.
这是代码:
import random
again = True
rolls = 0
while again == True:
rolls = float(raw_input("How many times would you like the dice to roll?"))
while rolls >= 1:
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print dice1 , dice2
rolls = rolls - 1
again = raw_input("Would you like to play again?")
if again == "Yes" or "Y" or "yes" or "y":
again = True
else:
again = False
Run Code Online (Sandbox Code Playgroud)
你们中的任何人都可以帮助我吗?
如果我有一个功能:
if a_string == "a" or a_string == "b" or a_string == "c":
# do this
Run Code Online (Sandbox Code Playgroud)
如果没有重复或陈述,我怎么能以不同的方式写出来?或者这是最好的方式?
我对在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中的其他一些列表中?
有没有办法有效地做:
if operator != ('+' or '-' or '*' or '/'):
Run Code Online (Sandbox Code Playgroud)
无需做
operator != '+' and operator != '-' and operator != '*'
Run Code Online (Sandbox Code Playgroud) 示例1 - 这有效:
def thisorthat():
var = 2
if (var == 3 or var == 2):
print "i see the second value"
elif (var == 2 or var == 15):
print "I don't see the second value"
thisorthat()
Run Code Online (Sandbox Code Playgroud)
示例2 - 这不起作用:
def thisorthat():
var = 2
if var == (3 or 2):
print "i see the second value"
elif var == (2 or 15):
print "I don't see the second value"
thisorthat() # "I don't see the second value"
Run Code Online (Sandbox Code Playgroud)
有没有办法将变量与"OR"运算符进行比较,而不是在每一行中重复两次变量?
我不知道是什么问题,但如果条件不足,我不能结束一个while循环.让我展示一下代码:
while True:
k = input ("Please enter 'hello': ")
if k == "hello":
m = input ("Are your Sure? (Y/N): ")
if m == "Y" or "y" or "yes" or "Yes":
break
Run Code Online (Sandbox Code Playgroud)
现在,在第二个提示之后,即使我输入no或任何其他内容,while循环仍然结束.我只想在第二次确认后结束它.编码中的错误是什么?
这是一个小程序,可用作算术计算器.我在这里读过以前的问题,但仍有疑问.在我的代码中,我在while循环中使用了'is'而不是==,但我的循环并没有停止.这有点出乎意料,因为如果用户在被要求输入时按'n',则变量ask将被新对象分配.如果有人可以查看代码并提供帮助,我将不胜感激.
def Add(x,y):
add = x+y
print("Answer:",add)
def Sub(x,y):
sub = x-y
print("Answer:",sub)
def Mult(x,y):
product = float(x*y)
print("Answer:",product)
def Div(x,y):
if y!=0:
div=float(x/y)
print("Answer:",div)
else:
print("Invalid input!")
ask='y'
while(ask is 'y' or 'Y'):
x=float(input("\nEnter x:"))
y=float(input("Enter y:"))
print("\nCALCULATOR:")
print("\nPlease select any of the following options:")
print("1.Add")
print("2.Subtract")
print("3.Multiplication")
print("4.Division")
opt=int(input("\nYour option:"))
if(opt is 1):
Add(x,y)
elif(opt is 2):
Sub(x,y)
elif(opt is 3):
Mult(x,y)
elif(opt is 4):
Div(x,y)
else:
print("Invalid option!")
ask=input("\nDo you want to continue?(y/n or Y/N)")
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个函数,该函数将对给定数据集中的商品进行分类(我知道这是非常简单的方式)。
看起来像:
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 ×10
if-statement ×2
boolean ×1
for-loop ×1
in-operator ×1
list ×1
or-operator ×1
string ×1
syntax ×1
while-loop ×1