Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimisation
#1
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?
Reply
#2
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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?
Reply
#4
Save it to a file. Python is not really designed for entering lots of code from the command line.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help Getting Started - Optimisation Problem lummers 1 1,699 Sep-19-2020, 05:05 AM
Last Post: perfringo
  Optimisation problem - star identification pberrett 1 2,226 Apr-01-2019, 12:17 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020