4

I am having a hard time passing the username which I have prompted with input in my main function, into a string in the next function.

I have already built a function program which passes a pre determined name into the next functions string message. But now that I have tried to step it up and use the input method, I am having a hell of a time passing it into the string message in my next function.

def main():
    someMessage = input("Enter your name:")
    return someMessage

def buildGreeting (someMessage):
    message = "Greetings " +input(someMessage)+ " you have been hacked! This message will self destruct in ten seconds!"
    return message
def printMessage(aMessage):
    print(aMessage)

if __name__ == '__main__':
    main()

I want it to say "Greetings Leif, you have been hacked! This message will self destruct in ten seconds!"

This is my current result. It prompts for my name and then does nothing further. This is what it reads when I run the program.

Enter your name:Leif

Process finished with exit code 0

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
25toLeif
  • 45
  • 1
  • 4
  • You should follow a python tutorial. This is a fundamental basic, helping you with this won't help in the long run. – Error - Syntactical Remorse Jun 11 '19 at 00:21
  • I don't know why it has a big space in the middle of my message but I am sure you all can tell what it should be. Thank you for any help. I have scoured the internet in search of an example and can not find what I am looking for. – 25toLeif Jun 11 '19 at 00:22
  • Possible duplicate of [Python: Passing variables between functions](https://stackoverflow.com/questions/16043797/python-passing-variables-between-functions) – Error - Syntactical Remorse Jun 11 '19 at 00:23
  • I am just following the instructions I was given which was to create a main function which gets called automatically when it runs, then create a greetUser function which accepts a string value as it's one and only parameter. The greetUser function will return a string message created using the string argument passed in. The main function will then print the string returned by greetUser. I've done this but I would like to do it with the input method now. – 25toLeif Jun 11 '19 at 00:28
  • Thank you syntactical remorse that's very helpful. I will read that post and see if I can comprehend this the right way. I know it's fundamental to my learning and I would like to not cripple myself in the long run, so I hear you loud and clear. – 25toLeif Jun 11 '19 at 00:30

3 Answers3

5

You will want to do it like this because main needs to call the other functions in order to print anything:

def buildGreeting():
    name = input("Enter your name:")
    message = "Greetings " + name + " you have been hacked! This message will self destruct in 10 seconds."
    return message

def printMessage(aMessage):
    print(aMessage)

def main():
    message = buildGreeting()
    printMessage(message)

if __name__ == '__main__':
    main()

When I run it:

[dkennetz fun]$ python destruct.py
Enter your name:Dennis
Greetings Dennis you have been hacked! This message will self destruct in 10 seconds.

PS the message doesn't self destruct.

d_kennetz
  • 5,219
  • 5
  • 21
  • 44
  • That's why I was so frustrated. I knew I was on the right path but knew something was out of place. I have a long ways to go of course and I thank you for your pointing it out. In this case I needed a visual. I will hit this topic much more to further solidify it in my brain. I am not getting something at a very basic level here and I am driving myself crazy over it. – 25toLeif Jun 11 '19 at 00:33
  • We all have to start somewhere! It's important to understand that `buildGreeting()` returns a string but doesn't require any arguments. When the program runs, it will ask you for input (which is name) and then this will be added to your `message` variable in the same function. We then return the value of message (which is a string). So when we call `buildGreeting()` in main we are saving the `return` value as a variable called message, which we then pass to `printMessage(message)`, which then prints your message out. – d_kennetz Jun 11 '19 at 00:38
  • 1
    Rad! If I could get an explanation like that where I need it from here on out, I will comprehend this much quicker. At least until I retrain my brain to think like this better. Cool now I am off to build the temperature conversion program using functions as well. Wish me knowledge and understanding lol – 25toLeif Jun 11 '19 at 00:43
4

Try this:

def main():
    someMessage = input("Enter your name:")
    buildGreeting(someMessage)

def buildGreeting (someMessage):
    message = "Greetings " +someMessage +" you have been hacked! This message will self destruct in ten seconds!"
    printMessage(message)

def printMessage(aMessage):
    print(aMessage)

if __name__ == '__main__':
    main()

The +input(someMessage) in your buildGreeting function is also not necessary since you already have that input passed to the function.

TheQuestioner
  • 724
  • 2
  • 8
  • 13
  • Thank you as well! My teacher will have my keister if I don't put my main first like yours, which I still don't fully understand yet. But thank you for the visual which I needed. – 25toLeif Jun 11 '19 at 00:47
0

Another approach,

def main():
    someMessage = input("Enter your name:")
    return someMessage
    
someMessage = main()

def buildGreeting(someMessage):
    message = "Greetings " +someMessage+ " you have been hacked! This message will self destruct in ten seconds!"
    return message
    
aMessage = buildGreeting(someMessage)


def printMessage(aMessage):
    print(aMessage)
   

if __name__ == '__main__':
    printMessage(aMessage)
Subham
  • 397
  • 1
  • 6
  • 14