Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing user input() from main() func into a string in the next function

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

like image 557
25toLeif Avatar asked Dec 31 '22 21:12

25toLeif


2 Answers

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.

like image 108
d_kennetz Avatar answered Jan 13 '23 23:01

d_kennetz


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.

like image 45
TheQuestioner Avatar answered Jan 13 '23 23:01

TheQuestioner