我想比较两个字符串,例如if self.Ends == '101100' or '001101'. self.Ends是一个比较两个3D坐标的课程.它可能会持有000100.有八个if elif比较.
程序总是返回一个'a'偶数self.Ends保持另一个01字符串.
if self.Ends == '100101' or '101100':
P_line.Line = 'a'
elif self.Ends == '000100' or '100000':
P_line.Line = 'b'
Run Code Online (Sandbox Code Playgroud) 你能告诉我为什么这段代码:
if (x and y) and z > 0:
return True
Run Code Online (Sandbox Code Playgroud)
是不一样的:
if (x and y) > 0 < z:
return True
Run Code Online (Sandbox Code Playgroud)
基本上,为什么我不能链接几个变量,如:
if var1 and var2 and var3 and var4 > 0:
do_this
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
--edit-- 我想我只想知道如何在不写作的情况下缩短条件:
if var1 > 0 and var2 > 0 and var3 > 0
Run Code Online (Sandbox Code Playgroud)
特别是如果条件很长,如:
if var1 > (pow(x, 3) / 2.5*pow(y,0.5)+x*y)
Run Code Online (Sandbox Code Playgroud)
如果重写每个变量的条件是不可行的,想象一下我是否有10个变量.或者有没有更好的方法,我没有看到可以在这样的情况下使用?谢谢你的回答!
我正在做一个项目,在某个时候会问用户一个是/否问题。我目前使用此代码来处理此类问题:
def yn():
global finalchoice
choice=str(raw_input("Y/N: "))
if choice == "Y":
finalchoice="true"
elif choice == "y":
finalchoice="true"
elif choice == "N":
finalchoice="false"
elif choice == "n":
finalchoice="false"
else:
yn()
pass
Run Code Online (Sandbox Code Playgroud)
但这似乎效率很低,特别是在我必须分别检查“Y”和“y”或“N”和“n”的情况下。我试过了:
if choice == "Y" or "y":
finalchoice="true"
Run Code Online (Sandbox Code Playgroud)
不幸的是,所有这些都忽略了“else”命令,并且会传递我给它的任何内容。
有小费吗?
我只是想知道为什么循环在满足这些条件并且过滤到我的其他函数时不会中断?我通过做一个真正的循环来修复它,并且只是打破每个if语句,但我想知道这样做有什么问题.
def main_entrance():
print "\n\tYou are in the main entrance. It is a large room with"
print "\ttwo doors, one to the left and one to the right. There"
print "\tis also a large windy stair case leading up to a second floor."
print "\n\tWhat are you going to do?\n"
print "\t #1 take the door on the left?"
print "\t #2 take the door on the right?"
print "\t #3 take the stairs to the second floor?"
choice = …Run Code Online (Sandbox Code Playgroud) 得到错误,但我不知道为什么......(我正在学习)我的代码;
import random
input("Hit enter to roll the dice")
global answer
def rollDice():
result = random.randrange(1,6)
print ("It landed on.." + str(result))
answer = input("would you like to play again? [y/n]")
rollDice();
if (answer == "y" or "Y"):
rollDice();
Run Code Online (Sandbox Code Playgroud)
错误; (一些剧本的作品)
Hit enter to roll the dice
It landed on..5
would you like to play again? [y/n]y
Traceback (most recent call last):
File "diceRoller.py", line 11, in <module>
while (answer == "y" or "Y"):
NameError: name 'answer' is not defined
Run Code Online (Sandbox Code Playgroud) 我一直收到这个错误,我完全不知道为什么,我有 3 个文件相互导入信息,但是这个主要文件只从 Monster_01.py 及其 player.py 中提取信息。
TypeError: player_attack() missing 1 required positional argument: 'self'
Run Code Online (Sandbox Code Playgroud)
---主要代码---
'''Player class
Fall 2014
@author Myles Taft (mbt6)
'''
import random
import monster_01
class Player():
def __init__(self, strength = 0, player_hp = 0):
self.strength = 1
self.player_hp = 100
print(self.strength, self.player_hp)
def battle():
def player_attack(self):
print('success')
while self.enemy_hp > 0:
monster_01()
self.dice1 = random.randint(1,6)
self.dice2 = random.randint(1,6)
self.dice_sum = dice1 + dice2
self.attack = dice1 + dice2
self.decide_roll = input('Type "roll" to roll the …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个代码库,我可以在其中存储随着时间的推移找到的代码片段并检索它们.到目前为止,这是我的代码.
# Python Code Library User Interface
import sys
class CodeLib:
codes = []
option = ['help (-h)', 'list (-l)', 'import (-i)', 'quit (-q)']
#What does the user want to do?
print "Welcome to the Code Library"
print "What would you like to do? \n %s" % option
choice = raw_input()
print choice
if choice == 'quit' or '-q':
sys.exit()
length = len(choice)
# Getting name of file to import if user chooses import before
# viewing the code list …Run Code Online (Sandbox Code Playgroud) ((针对上述编辑,上述链接未对此进行回答.上述问题与我的预期用途无关.))
我读过一个关于将字符串变成小写的类似问题;
我理解这是如何完美的,但是我自己的尝试失败了.
这是我当前调试块的设置示例;
#Debug block - Used to toggle the display of variable data throughout the game for debug purposes.
def debug():
print("Would you like to play in Debug/Developer Mode?")
while True:
global egg
choice = input()
if choice == "yes":
devdebug = 1
break
elif choice == "Yes":
devdebug = 1
break
elif choice == "no":
devdebug = 0
break
elif choice == "No":
devdebug = 0
break
elif choice == "bunny":
print("Easter is Here!")
egg = …Run Code Online (Sandbox Code Playgroud) 为什么这两个等式在Python中会产生两种不同的结果?我不确定为什么第一个输出零; 我在期待一个True.
>>> 0 and 2 >= 0
0
>>> 2 and 0 >= 0
True
Run Code Online (Sandbox Code Playgroud) 表达式if (mask1 | mask2) is None:返回此错误
TypeError: unsupported operand type(s) for |: 'int' and 'NoneType'
Run Code Online (Sandbox Code Playgroud)
如何检查两个变量之一是否为None?
python ×10
if-statement ×2
python-2.7 ×2
dice ×1
input ×1
logic ×1
lowercase ×1
math ×1
python-3.x ×1
text ×1
windows ×1