I have configured Sublime text 3 to compile and execute Python 3.5 code and have successfully run several small programs. However when I try and run this simple code to calculate the square of a number the user can input, the console will not return the final answer:
def square():
num = int(input("Please enter the number you wish to square: "))
answer = num ** 2
print (answer)
square()
In Sublime Text 3 it will ask the user for input but then not print the answer. If I run it in IDLE though it will print the answer. As I said I can run other small programs involving print (like Hello World for example) so I am not sure what is wrong. All help appreciated I am just starting out so please forgive my lack of experience.
I was able to run your code through my terminal. It ran perfectly. I wrote the same code in Sublime Text 3 as I use the same editor. I don't think there is any input/output exception for sublime. You might want to restart the terminal and then try creating a new .py file to run this code.
Related
I am trying to run a chunk of python code in Rstudio using the reticulate package. Be it a standard python script or RMarkdown, the Rstudio console shoots through the code and does not stop to ask me for input whenever it runs an input command.
Here's an example:
print('How much do you think Zuck owns?')
Zuck = int(input("Enter Zuck's net worth: "))
if Zuck < 5000000000:
print("That ain't much!")
elif Zuck == 5000000000:
print("That's 5 billion!")
else:
print("Ima make more after I graduate")
If I were running this code in Spyder/Jupyter notebook, the code will stop at the input command and ask me to enter Zuck's net worth before proceeding. Instead, here's what it does in Rstudio:
It is not stopping at the input command for me to enter Zuck's net worth. It rather goes ahead with the if statement, without assigning any value to the input - thus generating an error.
The same problem happens even in RMarkdown python code chunk.
Please let me know if there is any way for this to work.
I have Python 3.8(32-bit) installed, and I am using Atom to write my attempts and then copy-pasting them into the Python terminal.
The following code is copied directly from the very beginning of an introductory Python course I am taking "for fun":
n = 5
while n > 0:
print(n)
n=n-1
print('Blastoff!')
The code works in every sandbox I can find, and the last line works on its own in my terminal. But when I copy it to my terminal, I get an invalid syntax error that points to the word print. I can fix this and get the desired output by changing my code to:
n = 5
while n > 0:
print(n)
n=n-1
else:
print('Blastoff!')
But I have three issues with this:
Why does my original code not work, as it is copied directly from the course?
I need to hit Enter twice after copying in that second block of code for it to run. Why is that?
Why does Atom insist on indenting the last print farther than my other indents?
Here is what I am seeing when entering my first code brick:
>>> n = 5
>>> while n > 0:
... print(n)
... n=n-1
... print('Blastoff!')
File "<stdin>", line 4
print('Blastoff!')
^
SyntaxError: invalid syntax
Since you're entering code into the Python interpreter, it'll interpret the code line-by-line. This is great for quick tests and checks, but for larger code, you'll want to run an entire file.
You can achieve this a couple different ways:
Running it from the command prompt/terminal. If your Python executable is in your PATH, you can open a command prompt and navigate to your file and run python myfile.py. See "How to add Python to Windows PATH".
If you've installed Python from python.org, you may have IDLE installed. You can run the IDLE application and open your file from the menu File > Open. From there, you can run the file from the menu Run > Run Module.
I'd suggest the second option since you are learning and it will help you focus on coding instead of fighting your code environment. However, feel free to revisit option #1 in the future. It is definitely helpful to know your way around the command line (if you work on a machine without IDLE installed, this would be the proper way to run Python files).
Also "How to Run Your Python Scripts" is a great resource for learning more about how running scripts in Python works.
I'm very new to python/ programming in general
I'm making a script in which the user is given a random square number & has to report the square root of that number
The script itself is working fine, assuming I save the file within sublime, then run it separately through IDLE
When I press Ctrl + B, the script runs within sublime, but i'm unable to input anything (I type the number and press enter). In IDLE, typing the answer and pressing enter would show the next line
my input function itself is
x = int(input("What is the square root of "+str(b)+"? "))
What is your question? Is it input not showing through launch with command line or through IDLE?
Something like this?
import random
import math
given = random.randint(0,10) ** 2
print(given)
answer = input(f'Whats is the square root of {given}? ')
print('The square root is ', answer)
if math.sqrt(given) == int(answer):
print('This is correct')
else:
print('Wrong answer')
It is working fine for me both through command line, and in IDLE.
Unfortunately, Sublime Text does not support inputting data into a program. You will have to continue running your program separately. Or switch to a better IDE such as Visual Studio Code, which can be found here:
https://code.visualstudio.com
I tried executing this code from a Python 3.x book but the Python 3.8 shell will only execute the first two lines of code. If I remove the second line of code it will execute the code that is still there just fine for some reason. Is this a bug or is there something wrong with the code? I have tried restarting the shell and executing the code from a new file but nothing seems to be helping.
print("hello")
firstname=input("what is your first name? ")
print("thanks.")
surname=input("and what is your surname? ")
This code works fine for me. You have to fill out the first input form before the second one will become available. And make sure you are hitting enter after you enter the text.
I am new to python and pycharm.
I am writing a program that takes years to run, so I want to see the iteration number while debugging.
this is my code:
import urllib2
for i in range(n):
print i
responses[i]=u2.urlopen(urls[i])
(I have an array of n urls)
so, when I run it I see the outputs:
0
1
2
3
etc
but when I am debugging I don't see the output.
any idea anyone?
Its a simple code block and there should not be any problem with Pycharm debugger. I suggest to use latest PyCharm version 5.0.
Right click on your python file and select 'Debug yourPythonFile.py'.It should print value of i.
Just a small suggestion, use (responses.append(u2.urlopen(urls[i])) instead of responses[i]=u2.urlopen(urls[i])