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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With