我想使用我的原始输入使用我的函数

时间:2017-03-29 19:26:26

标签: python function raw-input

我有一个程序,我一直在努力查看我是否可以使用我的函数的原始输入

class object:
# this shows that x is on top of y
    def on(self,x,y):
        self.x = x
        self.y = y
        x = 1
        y = 0

        if x > y:
            print True
        else:
            print False
# this clears x making nothing be on top of x   
    def clear(self,x):
        self.x = x
        x = None
        print(x)
#  this shows that x is bigger than y 
    def heavy(self,x,y):
        self.x = x
        self.y = y
        x = 1
        y = 0

        if x > y:
            print True
        else:
            print False

    input = raw_input("Enter an object: (Type stop to stop the loop): ") # raw input allows me to write string without the quotes
    obj = "" 
    while input != ("stop"):
        obj = obj + " " + input
        input = raw_input("Enter an object: ")
    print ("These are the objects: " + obj)
# this is a loop that allows the user to enter as many objects until they type stop

我想要发生的是当用户输入两个单词时,我可以使用我创建的功能。例如,如果我使用on()(第一个函数)它将显示(on(word1,word2),如果我使用clear它将显示(清除(word1)清除第二个单词。我只是想知道我是否在正确的轨道。希望很好解释谢谢 希望成为这个伟大社区的一部分:)

1 个答案:

答案 0 :(得分:0)

如果我理解你的要求,请看看下面的内容。

下面我有4个已定义的变量。其中3个将执行不同的任务。第四个用于确定要使用的变量。我希望这能指出你正确的方向。

def random_words(word,word2):
    print (word +"\n"+"First"+"\n"+ word2)

# you can decide how to print each string given. 
# This will allow you to place one on top of the other. 
# the "\n" will print the following string on the next line.

def other_words(word,word2):
    print (word +" Second "+ word2)

def more_words(word,word2):
    print (word +" Third "+ word2)

def select_def(sdef,word,word2): # This variable will decide the others
    if sdef == "1":
        random_words(word,word2)
    elif sdef == "2":
        other_words(word,word2)
    else:
        more_words(word,word2)

var1= str(input("Enter a number -->"))
var2= str(input("Enter a Word -->"))
var3= str(input("Enter another -->"))

select_def(var1,var2,var3)

调用函数的示例。

select_def("2","Makin","Bacon") #You can use something like this in an input function.

结果:

>>> 
Makin Second Bacon

第二个例子:

select_def("1","Makin","Bacon")

结果:

>>> 
Makin
First
Bacon