Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改if状态下的等效值_Python_Python 3.x - Fatal编程技术网

Python 更改if状态下的等效值

Python 更改if状态下的等效值,python,python-3.x,Python,Python 3.x,这是我的密码: com=input('> ') while True: if com=='say': print('1') com=input('> ') if com=='change': global change_com change_com=input('changeing_say_to: ') change_com='say' com=input('> ')

这是我的密码:

com=input('> ')
while True:
    if com=='say':
        print('1')
        com=input('> ')
    if com=='change':
        global change_com
        change_com=input('changeing_say_to: ')
        change_com='say'
        com=input('> ')
我想得到的结果是,在
change\u com=input('change\u say\u to:')
之后,
change\u com
变成除了
say
之外的东西,比如
NAME
,然后当我给
NAME
而不是
say
赋予
com
的值时,如果com='say'/code>状态,它将进入
。我该怎么做

目标如下:

        > say
        1
        > change
        changeing_say_to: NAME
        > NAME
        1
这里有一个解决方案:

value='say'
while True:
    com = input('> ')
    if com == value:
        print('1')
    elif com == 'change':
        value=input('changeing_say_to: ')
这里有一个解决方案:

value='say'
while True:
    com = input('> ')
    if com == value:
        print('1')
    elif com == 'change':
        value=input('changeing_say_to: ')

您可以与
change\u com
的当前值进行比较,而不是硬编码的
say
-并将嵌套的
输入(“>”
)调用移动到while循环的开头。还提供一种离开循环的方法(在Ctrl-C旁边):

您可以使用字符串格式参数调整消息,使其与
change\u com
中当前的内容相匹配

输出:

> say
1
> say
1
> change
changing say to: tut
> tut
1
> tut
1
> change
changing tut to: lol
> lol
1
> break

您可以与
change\u com
的当前值进行比较,而不是硬编码的
say
-并将嵌套的
输入(“>”
)调用移动到while循环的开头。还提供一种离开循环的方法(在Ctrl-C旁边):

您可以使用字符串格式参数调整消息,使其与
change\u com
中当前的内容相匹配

输出:

> say
1
> say
1
> change
changing say to: tut
> tut
1
> tut
1
> change
changing tut to: lol
> lol
1
> break