Python Forum
Cannot get simple i/o to function.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot get simple i/o to function.
#1
My first post. I am new to Python and am using Sublime Text Editor with Python 3. Sample code below was pasted from a tutorial file, io.py
It runs, and it asks me to type something. I type a string into the response box and hit enter. Nothing happens.
I have been running numerous learning files w/o problem. Just cannot get i/o to work properly. It seems I need to do something special to get i/o running. Haven't created any projects yet. just small files. I wrote a program to print out all the prime numbers from 2 to a million, for example, without difficulty.

All help would be appreciated.


txt = input("Type something to test this out: ")

# Note that in version 3, the print() function
# requires the use of parenthesis.
print("Is this what you just said? ", txt)
Reply
#2
you need to format the string and you can do it multiply ways:
print("Is this what you just said? %s" %(txt)) # first
print("Is this what you just said? {0}".format(txt)) #second
print(f"Is this what you just said? txt") # 3.7 and higher
Reply
#3
Thx joe_momma for suggestions, but none of the four bottom lines work.

My original program functions on Android Pydroid 3,
but I cannot get Sublime Text versions on W10 to work with either of my two PCs (desktop or laptop).

txt = input("Type something to test this out: ")
# print("a) Is this what you just said? txt") # does not work on pc with Sublime Text; works on Pydroid 3 for Android
# print("b) Is this what you just said? %s" %(txt)) # first suggestion by joe_momma (fails with Sublime Text)
# print("c) Is this what you just said? {0}".format(txt)) # second
printf(f"d) Is this what you just said? txt") # third 3.7 and higher suggested by joe_momma
Reply
#4
@jerryi: Your code in the original post as well as joe_momma's code is syntatically correct Python, so it should work. If it doesn't work on your system with, it's a specific problem of your system.

How do you run the code? In case you run it on Win by double-clicking the .py file, you may never see the output, as the window is closed right after the print function finished - which is faster you than you can read. Open the cmd line instead and type python3 io.py, than the cmd window remains open and you can read the output.

Regards, noisefloor
Reply
#5
as @noisefloor said - all 4 snippets should work. It's not clear what you mean by "I type a string into the response box and hit enter." what "response box"? Follow @noisefloor advice to run the script from cmd
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
#6
In this thread "Is this a bug?"
i found this answer
Quote:When you execute code in Sublime (either via a build system or interactively in the console), there is no way to capture input. More technically, the console is connected to stdout and stderr but not stdin. So it’s not possible to run an interactive program from within Sublime directly.
Reply
#7
I appreciate the help I have received and am progressing gradually. I hsve a goal to teach a few inner-city students some elementary coding this summer, and have selected Python3 as a teaching platform. I am a retired mechanical engineer, but I am completely new to the language, I already appreciate its value and appeal. Because most of these students are refugees from places like Burma Thailand, Nepal, etc., they do not have access to laptop computers for our class. So I decided to put Python 3.7.3 on a few older laptops that I obtained for almost-free, and to use Sublime as our editor and work platform. That seems to be working fine for my desktop PC at home and for my Lenovo Ideapad. Both installations were fairly easy to accomplish.

However, I am not progressing as well in setting up an old Dell Latitude D630 with W10. I cannot seem to get program output through Sublime: Only an indication of elapsed time. I am in the dark re. what to do to configure Sublime properly for functionality. I can run a snippet through the command prompt, so Python 3 is doing its job, but Sublime will not even spit out a response to print("Hello"). When I hit ctrl-b, it just responds [Finished in 0.6 s], with no Hello greeting.

 print("Hello") 
Reply
#8
If you're acting legal and respect the licensing, it's expensive to buy copies of Windows.
I don't use sublime, but if you google for CTRL + B,, sublime would only
compile the code, not run it. It's a bit uncommon for Python, because this step is implicitly done by Python itself.
CTRL + Shift + B should run the code, but you should also know how to run
a Python program in a terminal. Since Python 3.5 the program py.exe is delivered. This is just a runner, which
finds the right interpreter on your system. So, if you're teaching Python on Windows, you should know this.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
Thank you, DeaD-EyE. I fumbled around and discovered that if I click Tools > Build System > Python
and then click Tools > Build > Ctrl-B and then click Build > Results > Show Results and then hit escape to get rid of the old results in the bottom part of my screen, then ctrl-b becomes functional. I have no idea what this is all about or why it seems so convoluted to get everything to function, and I am not sure which of my steps were necessary and which ones were not. And I could not find a 'coherent-to-me' explanation, but I will now consider the issue resolved. Maybe someday I will find enough explanations to understand what is going on. Meanwhile, I will truck on, and warn the kids not to mess with the Tools options when things aren't broken.
Reply
#10
Maybe just watch a tutorial how to set up Sublime Text
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  got SyntaxError while building simple function zarize 2 2,130 Feb-14-2020, 10:51 AM
Last Post: zarize
  Simple statistics with range function Pythonlearner2019 2 2,128 Nov-25-2019, 05:25 PM
Last Post: Pythonlearner2019
  Need help with a simple function WorldPark 4 2,661 Apr-26-2019, 12:28 PM
Last Post: perfringo
  Why this simple function doesnt work? blackknite 8 4,011 Jan-05-2019, 12:32 PM
Last Post: buran
  Simple Function Problem, please help ShadowWarrior17 16 7,079 Jan-03-2018, 09:29 PM
Last Post: ShadowWarrior17
  How to do multithrading of a simple function? ratanbhushan 1 2,636 Nov-17-2017, 02:32 PM
Last Post: heiner55
  Simple Function Call PappaBear 2 3,174 Apr-04-2017, 11:12 PM
Last Post: PappaBear

Forum Jump:

User Panel Messages

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