-4

Just started learning. Put the code:

username = input("Enter username:")
print("Username is: " + username)

into IDLE and after it asks for input, I expect it to print something. It doesn't. It just asks for input and that's it. What am I missing?

Just to clarify to everyone- I had entered the code on separate lines and it says

Enter Username:

I enter 'John'

Nothing happened after that.

Edit: I think the answer is that you cannot do multi-line code in the shell. I had no idea what a 'shell' is as opposed to a file where you can do multi-line.

  • You have to enter a value. This value will be assigned to 'username'. Then it will print that value. – Maleehak Jan 07 '21 at 16:40
  • They were on separate lines. The formatting just got messed up when I posted the question – Jimmy Coltrane Jan 07 '21 at 16:41
  • It says IDLE shell 3.9.1 It came with the python installer/launcher – Jimmy Coltrane Jan 07 '21 at 16:49
  • If I resubmit the print function (after entering the input), it works. But not when I submit both those lines at once. – Jimmy Coltrane Jan 07 '21 at 16:51
  • 1
    What do you mean by resubmit? How are you running the code? By creating a separate file or by directly writing on shell? – Maleehak Jan 07 '21 at 17:05
  • 1
    You hit enter right? – Dock Jan 07 '21 at 17:08
  • 2
    @JimmyColtrane You cannot paste multiple lines at once into an interactive shell. You need to enter and execute each line separately. Alternatively, in IDLE, do Ctrl+N (new window), Ctrl+V (paste), Ctrl+S (save file), F5 (run). Or install IPython and use its %paste command. – ekhumoro Jan 07 '21 at 17:48
  • Right, ok. I think that's the answer, ekhumoro, thanks. I read a few 'getting started with IDLE' articles and they didn't mention that. Maleeha, I meant if I do it line by line it works. – Jimmy Coltrane Jan 08 '21 at 18:48

1 Answers1

2

Python is a top-down language, meaning that it will only continue to the next line of code until the current line of code has finished running

The input() function requires that you provide input into the console, to continue to the next line of code. So, you have to input something and only then will it actually print anything.

DapperDuck
  • 2,728
  • 1
  • 9
  • 21
  • @jimmycoltrane You can accomplish running both at the same time using multiprocessing, threading, etc. I wouldn't recommend that for a beginner. – Dock Jan 07 '21 at 16:41
  • Hi, thanks. Just to clarify, I had entered the code on separate lines. Also, I had entered a username. Nothing happened after that. – Jimmy Coltrane Jan 07 '21 at 16:43
  • @JimmyColtrane You entered it into python IDLE and nothing happened? That's extremely strange – DapperDuck Jan 07 '21 at 16:46
  • Yes, if you see the question now, I've clarified what happened. After I enter a username, nothing happens. – Jimmy Coltrane Jan 07 '21 at 16:47
  • I don't know how that's even possible! Perhaps some issue with your python installation? – DapperDuck Jan 07 '21 at 16:49