Python - 减少长if-elif代码行

时间:2016-09-24 06:27:13

标签: python python-3.x

我刚刚开始学习python,当我的代码工作时,我想减少我的代码行。我正在考虑使用list方法,但我无法真正找到解决方案。我事先尝试过搜索,但我找不到与我有关的东西。

对于我的代码,它就像是从一个空间移动到另一个空间的点。可能的运动由方向决定。我为确定点的移动所做的是分配userPoint(确定点的位置)。为了移动,它必须满足空间设置的条件(也就是userInput.upper()表示的唯一可用方向),否则它将不会移动并打印输入无效。

if userInput.upper() == 'QUIT':
    break
elif userPoint == 0 and userInput.upper() =='EAST':
    userPoint = 1
elif userPoint == 1 and userInput.upper() == 'WEST':
    userPoint = 0
elif userPoint == 1 and userInput.upper() == 'EAST':
    userPoint = 2
elif userPoint == 1 and userInput.upper() == 'SOUTH':
    userPoint = 4
elif userPoint == 2 and userInput.upper() == 'WEST':
    userPoint = 1
elif userPoint == 3 and userInput.upper() == 'SOUTH':
    userPoint = 6
elif userPoint == 4 and userInput.upper() == 'NORTH':
    userPoint = 1
elif userPoint == 4 and userInput.upper() == 'EAST':
    userPoint = 5
elif userPoint == 5 and userInput.upper() == 'WEST':
    userPoint = 4
elif userPoint == 5 and userInput.upper() == 'SOUTH':
    userPoint = 8
elif userPoint == 6 and userInput.upper() == 'NORTH':
    userPoint = 3
elif userPoint == 6 and userInput.upper() == 'EAST':
    userPoint = 7
elif userPoint == 7 and userInput.upper() == 'WEST':
    userPoint = 6
elif userPoint == 7 and userInput.upper() == 'EAST':
    userPoint = 8
elif userPoint == 8 and userInput.upper() == 'WEST':
    userPoint = 7
elif userPoint == 8 and userInput.upper() =='NORTH':
    userPoint = 5
else:
    print('Please input a valid direction.\n')

非常感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

我将通过创建字典映射来完成它 像这样的东西

<?php
    session_start();

    $conn = pg_connect("host=localhost port=5432 dbname=alc user=postgres password=postgres");

    $training_id= "SELECT m.training_mst_id FROM tbl_user u, training_mst m WHERE u.user_id = m.trainer_id AND m.start_traning >= now() AND m.end_traning <= now() AND m.trainer_id = ".$_SESSION['user_id'];
    $result = pg_query($conn,$training_id);

    if( $result ){

        $data=pg_fetch_object( $result );
        $id=$data->training_mst_id;

        $query = "UPDATE training_mst set status='TRUE' where training_mst_id= ".$id;
        $result2 = pg_query($conn,$query);
    }

?>

答案 1 :(得分:0)

请注意,只要userInput为东,效果就是向userDirection添加一个。

您可以通过测试userInput并计算userDirection来缩短长行。

if userInput.upper == "EAST":
    userDirection += 1
elif userInput.upper == "SOUTH":
    userDirection += 3
elif #  etc.

答案 2 :(得分:0)

用户似乎正在经历一种看起来像这样的迷宫:

2--1--0
   |
5--4  3
|     |
8--7--6

我们可以将有效连接存储为集合列表。例如,{0, 1}表示从0到1以及从1到0的连接。集合没有排序,因此{0, 1}{1, 0}是相同的。

if userInput.upper() == 'QUIT':
    break

connections = [{0,1}, {1,2}, {1,4}, {3,6}, {4,5}, {5,8}, {6,7}, {7,8}]

if userInput.upper() == 'EAST':
    newUserPoint = userPoint + 1
elif userInput.upper() == 'WEST':
    newUserPoint = userPoint - 1
elif userInput.upper() == 'SOUTH':
    newUserPoint = userPoint + 3
elif userInput.upper() == 'NORTH':
    newUserPoint = userPoint - 3
else:
    newUserPoint = None

movementIsValid = {userPoint, newUserPoint} in connections
if movementIsValid:
    userPoint = newUserPoint
else:
    print('Please input a valid direction.\n')

要了解如何使用列表和集合,您可以查看教程(listssets)。在上面的代码中,我们计算新点,然后检查是否存在从旧点到新点的连接。

当我们计算新点时,我们可以摆脱一些重复。

if userInput.upper() == 'QUIT':
    break

directions = {'EAST': 1, 'WEST': -1, 'SOUTH': 3, 'NORTH': -3}
connections = [{0,1}, {1,2}, {1,4}, {3,6}, {4,5}, {5,8}, {6,7}, {7,8}]

if userInput.upper() in directions:
    userDirection = directions[userInput.upper()]
    newUserPoint = userPoint + userDirection
else:
    newUserPoint = None

movementIsValid = {userPoint, newUserPoint} in connections
if movementIsValid:
    userPoint = newUserPoint
else:
    print('Please input a valid direction.\n')

在此解决方案中,我们使用dictionary作为指示。

我已经使用了许多可能对您来说很新的概念,您可以在我给出的链接中阅读它们,如果您遇到任何问题,可以在StackOverflow上提出新问题。