如何将输入作为变量参与?

时间:2017-03-20 17:40:49

标签: python python-3.x

你好Stackoverflow的人,我再说一遍。我正在创建一个基本的Ubuntu终端模拟器作为我学习Python的一个小小项目。

我需要参与输入并将其转换为变量,因此我可以将其添加到“命令输出”中。示例:如果有人放置“sudo add-apt-repository ppa:webupd8team / tor-browser”,代码将能够拉出“webupd8team”,并将其用作输出的一部分,然后拉动tor-browser,将其用作输出的一部分。

我已经有一些if / else过滤器检查某些命令(例如Sudo)但我无法真正完成某些事情的每个可能选项。

if "sudo" in originalInput: #Checks to see if "sudo" is in the command input. 
password = input("[sudo] enter password for evan:") #Creating another input if sudo is found in input1


if password == "1qaz2wsx":

  if "cd" in originalInput:
    print("This is here for testing purposes") #Haven't started working on the CD command yet
    CommandPrompt()

  if "add-apt-repository ppa:" in originalInput:
    confirm = input("You are about to add this PPA to your system. Confirm?")
      if confirm == "1":  #Just here to pressing enter will activate ppa_install(), function made to mimic PPA installation
      return True

      else:  
        ppa_install()
        CommandPrompt()

我已经为PPA命令设置了这个东西,但是我需要能够让代码拉出sudo / add-apt /之后的东西并在输出中使用它们。

1 个答案:

答案 0 :(得分:0)

您似乎需要拆分方法,该方法在给定的分隔符(空格是默认值)或正则表达式包中拆分字符串。

在这种情况下,快速,低技术的方法是将字符串拆分为空格并拉出" ppa:webupd8team / tor-browser"。用冒号和斜线除以您选择的任何字符串处理方式(查找,索引,字符串切片),以及您想要的字符串。

有关详细信息,请参阅documentation

相关问题