在switch case语句中为每个case添加多行(python)
我是编程新手,并试图理解如何在python中使用switch-case语句。我的问题是,当我试图使用一个语句时,每种情况下都有一行以上的语句,就会出现语法错误。这可能吗?我错过了什么?如果不可能,switch case语句如何有用 这是我用来测试的代码在switch case语句中为每个case添加多行(python),python,switch-statement,syntax-error,Python,Switch Statement,Syntax Error,我是编程新手,并试图理解如何在python中使用switch-case语句。我的问题是,当我试图使用一个语句时,每种情况下都有一行以上的语句,就会出现语法错误。这可能吗?我错过了什么?如果不可能,switch case语句如何有用 这是我用来测试的代码 drop = random.randint(1,3) inventory = [] def randomDrop(i): switcher ={ 1: "you got x"
drop = random.randint(1,3)
inventory = []
def randomDrop(i):
switcher ={
1:
"you got x"
inventory.append(x) #syntax error on this line
2:
"you got y",
inventory.append(y)
3:
"you got z",
inventory.append(z)
}
return switcher.get(i,"you got nothing")
randomDrop(drop)
Python不支持开关大小写。您必须使用if-elif-else插入开关大小写。Python不支持开关大小写。您必须使用if elif else插入开关盒。我相信您正在尝试这样做。如果有帮助,请告诉我
inventory = []
def randomDrop(i):
if i == 1:
inventory.append(x)
print('you got x')
elif i == 2:
inventory.append(y)
print('you got y')
elif i == 3:
inventory.append(z)
print('you got z')
else:
print('you got nothing')
drop = random.randint(1,3)
randomDrop(drop)
我相信你正试图这样做。如果有帮助,请告诉我
inventory = []
def randomDrop(i):
if i == 1:
inventory.append(x)
print('you got x')
elif i == 2:
inventory.append(y)
print('you got y')
elif i == 3:
inventory.append(z)
print('you got z')
else:
print('you got nothing')
drop = random.randint(1,3)
randomDrop(drop)
Python不支持开关大小写。你正在使用的东西是字典。它是一个关键的、有价值的数据结构。 dict的语法:{key1:value1,key2:value2} 但是您使用多个语句来代替value,这就是语法错误的原因
drop = random.randint(1,3)
inventory = []
def randomDrop(i):
switcher ={
1:
"you got x"
inventory.append(x) #multiple statement instead of value or reference
2:
"you got y",
inventory.append(y) #multiple statement instead of value or reference
3:
"you got z",
inventory.append(z) #multiple statement instead of value or reference
}
return switcher.get(i,"you got nothing")
randomDrop(drop)
如果需要,请使用if-else
inventory = []
def randomDrop(i):
if i == 1:
inventory.append(x)
return 'you got x'
elif i == 2:
inventory.append(y)
return 'you got y'
elif i == 3:
inventory.append(z)
return 'you got z'
else:
return 'you got nothing'
drop = random.randint(1,3)
randomDrop(drop)
见:
Python不支持开关大小写。你正在使用的东西是字典。它是一个关键的、有价值的数据结构。 dict的语法:{key1:value1,key2:value2} 但是您使用多个语句来代替value,这就是语法错误的原因
drop = random.randint(1,3)
inventory = []
def randomDrop(i):
switcher ={
1:
"you got x"
inventory.append(x) #multiple statement instead of value or reference
2:
"you got y",
inventory.append(y) #multiple statement instead of value or reference
3:
"you got z",
inventory.append(z) #multiple statement instead of value or reference
}
return switcher.get(i,"you got nothing")
randomDrop(drop)
如果需要,请使用if-else
inventory = []
def randomDrop(i):
if i == 1:
inventory.append(x)
return 'you got x'
elif i == 2:
inventory.append(y)
return 'you got y'
elif i == 3:
inventory.append(z)
return 'you got z'
else:
return 'you got nothing'
drop = random.randint(1,3)
randomDrop(drop)
见:
要获得更易于接受和维护的结构,您可以为交换机使用辅助功能:
def switch(v): yield lambda *c: v in c
这将允许您以类似C的风格编写代码:
for case in switch(i):
if case(1):
inventory.append(x)
return "you got x"
if case(2):
inventory.append(y)
return "you got y"
if case(3):
inventory.append(z)
return "you got z"
else:
return "you got nothing"
要获得更易于接受和维护的结构,您可以为交换机使用辅助功能:
def switch(v): yield lambda *c: v in c
这将允许您以类似C的风格编写代码:
for case in switch(i):
if case(1):
inventory.append(x)
return "you got x"
if case(2):
inventory.append(y)
return "you got y"
if case(3):
inventory.append(z)
return "you got z"
else:
return "you got nothing"
python不支持
开关
。您使用的是一个字典,它是一个用于保存数据或引用的数据结构。改用if-else
。python不支持switch
。您使用的是一个字典,它是一个用于保存数据或引用的数据结构。使用if-else
。这就是我试图做的,只是使用一个switch语句。我不知道Python不支持它们,这就是我试图做的,只是用一个switch语句。我不知道Python不支持它们。