[Tutor] Formatting Prompts
dn
PyTutor at DancesWithMice.info
Tue Jul 14 16:11:31 EDT 2020
On 14/07/20 9:07 PM, John Weller wrote:
> Hi Mike
>
>> The input() function allows for a prompt to be inserted to provide information to the
>> user. Can this string be formatted or does it just accept ascii text? I'm shooting for
>> something like:
>>
>> Counter += 1
>>
>> Input("This is attempt# ", counter)
>>
>
> Your code looks as though you are trying to output a string "This is attempt #47" or whatever whereas your input command is asking the user for an attempt number - at least that is what it looks like to me, no doubt Alan will correct me if I am wrong 😊
>
> If it is the case then your command should be print("This is attempt #" + str(counter)) I think.
Remember that if you open python3 at the command line, you can
experiment with the REPL: typing-in commands and watching them execute
immediately (or complain if there's an error). It's a very quick tool to
learn something new or check one's understanding!
There's no reason why a complicated "prompt" couldn't be embedded into a
preceding print(). Indeed print() offers some automatic formatting 'for
free'. However, remember that the default is that it will add an EOL
after the printed-string!
NB EOL/End-of-line varies between different OpSys.
input() (note the lack of upper-case!) performs two tasks. It takes a
single argument (a string) and outputs it to sysout (usually the screen
- for reasons that will become obvious), it then waits for input from
sysin (usually the keyboard), which it then returns to the code (as a
string). So, the usual presentation is:
result = input( "Any data? " )
Note the trailing space - the rationale for which can be discovered by
quick experimentation; also the question-mark which is a (minimal) UX
(user experience) feature.
https://docs.python.org/dev/library/functions.html?highlight=input#input
https://docs.python.org/dev/tutorial/controlflow.html?highlight=input
Much of @Alan's earlier advice appears in:
https://docs.python.org/dev/tutorial/inputoutput.html?highlight=input
and the more authoritative
https://docs.python.org/dev/tutorial/stdlib2.html?highlight=input
--
Regards =dn
More information about the Tutor
mailing list