我想在Python中编写一个函数,它根据输入索引的值返回不同的固定值.
在其他语言中,我会使用switch或case声明,但Python似乎没有switch声明.在这种情况下,推荐的Python解决方案是什么?
级别:初学者
def play_game(word_list):
hand = deal_hand(HAND_SIZE) # random init
while True:
cmd = raw_input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ')
if cmd == 'n':
hand = deal_hand(HAND_SIZE)
play_hand(hand.copy(), word_list)
print
elif cmd == 'r':
play_hand(hand.copy(), word_list)
print
elif cmd == 'e':
break
else:
print "Invalid command."
Run Code Online (Sandbox Code Playgroud)
我的问题:什么是真的?
我认为"虽然真实"是简写但是为了什么?而变量'hand'被赋值?如果变量'hand'没有赋值?该怎么办?