Python Forum

Full Version: Optimisation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Just a python noob wondering if there's a better method for delaying the order in which a program asks for an input, rather than just separating commands with ; for instance, in this simple program, the user is asked for the name of their favourite song, followed by the first, second, and third line of the song, and then this information is basically just printed back to them:

 song = input("What is your favourite song?\n");line1 = input("What\'s the first line?\n");line2 = input("And the second?\n");line3 = input("Finally, the third line?\n");print(f"\n{song}\n{line1},\n{line2},\n{line3}")
As you can see here, I have everything on one line separated with a semicolon (;), as if I was to press enter it would of course execute the line of code so far. Is there another way to separate commands like this without having everything on the same line?
(Dec-23-2019, 06:51 PM)H0M1C1D4L_P1G Wrote: [ -> ]as if I was to press enter it would of course execute the line of code so far
That is when you use interactive mode/shell (i.e. that is when you have >>>). Each line is evaluated immediately.
You should write your code and save it as file with py extension. In this case each statement will be on separate line

song = input("What is your favourite song?\n")
line1 = input("What\'s the first line?\n")
line2 = input("And the second?\n")
line3 = input("Finally, the third line?\n")
print(f"\n{song}\n{line1},\n{line2},\n{line3}")


Now there are other issues with your code (although it runs), but it's because there are better ways to do what you are doing here

look at https://python-forum.io/Thread-How-to-Ex...ython-code
Yeah I'm using the command shell, sorry, should have specified that. Is there a better way to do it in the actual command shell, or is it only if I use the interpreter?
Save it to a file. Python is not really designed for entering lots of code from the command line.