我希望有一个人可以帮助我。当我运行下面的函数时,无论输入什么,都会打印规则。我看不出我做错了什么。
def check_rules():
while True:
request = input("\nWould you like to know the rules? (y/n) ")
if request == "y" or "Y":
print("""
1. Each player takes it in turn to roll a dice.
2. The player then turns over a card with the same
number as the number rolled to see how many ladybirds
there are (0-3).
3. The player keeps the card.
4. If a player rolls a number that is not on an unclaimed
card, play continues …Run Code Online (Sandbox Code Playgroud) 我有一个变量,并想检查它是否与其他两个变量中的至少一个匹配.
显然,我可以这样做:
if a == b or a == c:
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更短的方式,例如:
if a == (b or c):
Run Code Online (Sandbox Code Playgroud)
如何测试变量是否与 - 至少 - 其中一个变量相同?
我真的很难评估布尔表达式.看代码:
def f(A):
if A=='a' or A=='b' or A=='c' ...:
return True
return False
Run Code Online (Sandbox Code Playgroud)
当A可以等于甚至更多的字符串时,有没有方便和优雅的方法来做到这一点?
这是通过询问用户他们想要了解哪个元素来查找元素详细信息的代码.问题是当我运行它时会打印所有的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) 我正在处理创建列表的代码,然后应用"或"和"和"条件来执行进一步操作:
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**应该是真实的,但它打印"不真实".这个错误的原因是什么?谢谢
下面的if语句成功,并且输出s的值。但是,如果删除:和/或空格条件,则它将失败。我对为什么它首先成功感到困惑。
s="( )"
if ("if" and ":" and " ") in s:
print(s)
Run Code Online (Sandbox Code Playgroud) 手头的任务很简单,制作一个简短的程序,要求提供cpsc先决条件(数字217,219或233)和数学先决条件(217或251),如果你有一个这样的类作为先决条件,那么应该说先决条件遇到,如果没有,则不满足先决条件.我的代码如下(不要笑,字面意思是最大的python noob):
cpsc=input("Which cpsc course have you taken (only pick one): ")
math=input("which math course have you taken (only pick one): ")
if cpsc==(217 or 219 or 233) and math ==(217 or 251)
print("prerequisites met")
else:
print("prerequisites not met")
Run Code Online (Sandbox Code Playgroud)
每个输入我有它只是给了我别的打印,我假设它的一个问题,在比较中cpsc==和math==,我能做些什么,使这项工作?任何和所有的帮助将不胜感激.
我刚刚开始使用python并且已经陷入了一些在我看来显然应该工作的东西.这是我的第一个代码,我只是尝试与用户进行对话.
year = input("What year are you in school? ")
yearlikedislike = input("Do you like it at school? ")
if (yearlikedislike == "yes" or "Yes" or "YES" or "yep" or "yup" or "Yep" or "Yup"):
print("What's so good about year " + year, "? ")
input("")
print("That's good!")
time.sleep(1)
endinput = input("I have to go now. See you later! ")
exit()
if (yearlikedislike == "no" or "No" or "nope" or "Nope" or "NOPE"):
print("What's so bad about year " + …Run Code Online (Sandbox Code Playgroud) 我遇到的问题是计算后缀形式表达式:例如,(1, 2, '+', 3, '*')。
通过使用以下算法计算表达式: 1. 如果表达式仅包含整数,则返回该整数。2. 否则,维护一个堆栈。循环遍历元组并将每个元素推入堆栈。如果该元素是运算符,则将前两个元素弹出堆栈,计算结果并将结果压入堆栈。为了说明这一点,我们以上面的例子为例。最初,堆栈是空的。测试用例是
calculate((1, 2, '*', 3, '-', 2, '*', 5, '+'))
3
Run Code Online (Sandbox Code Playgroud)
而我的第一个代码不好(硬编码和所有 >< ):
def calculate(inputs):
if len(inputs) ==1:
return inputs[0]
elif len(inputs) == 5:
s= []
push_stack(s, inputs[0])
push_stack(s, inputs[1])
if inputs[2] == '*':
A = s.pop() * s.pop()
elif inputs[2] == '+':
A = s.pop() + s.pop()
elif inputs[2] == '-':
A= s.pop() - s.pop()
elif inputs[2] == '/':
A = s.pop() / s.pop()
s.clear()
s= [A]
push_stack(s, inputs[3])
if …Run Code Online (Sandbox Code Playgroud) 我有这个 if 语句,必须检查这两个字符串是否存在,self.line.lower()例如:123abc567或blaxyzbla:
if 'abc' or 'xyz' in self.line.lower():
print ('Present')
else:
print ('Not Present')
Run Code Online (Sandbox Code Playgroud)
self.line.lower()如果不包含或abc,为什么这会返回 true(Print Present)xyz如果字符串中
我必须用它来工作:
if self.line.lower().find('abc') != -1:
print ('Present')
elif self.line.lower().find('xyz') != -1:
print ('Present')
else:
print ('Not Present')
Run Code Online (Sandbox Code Playgroud)
多谢。