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
Related
I developed a python program and convert it into .exe file using:
pyinstaller --onefile master.py
After that I compressed it into a Zip-File and made an installer for the program using NSIS. Then, I uploaded it on to my Google Drive.
I switched to a new computer(has no python on it) and downloaded my program from Drive. I installed it on the machine and tried to run it. It worked because I was able to type some stuff. This program is NOT GUI based yet, it asks user to enter some stuff on the Command Prompt. Once user presses enter after typing an input, the program gives me an error such as: ImportError: No module named selenium
import os, time
import threading
status = True
while status:
# number of windows user want
num_of_win = input("How many screen would you like to open?\nNumber of screens [1-10]: ")
try:
# convert it into an int
num_of_win = int(num_of_win)
# if number of windows is greater than 10
if num_of_win > 10:
print("Max number of screens you can have is 10. Please try again.")
# if number of windows is equal to or less than 10
elif num_of_win <= 10:
# brake the loop
status = False
# open cmd windows num_of_win times
for x in range(num_of_win):
# this is path to index.py with parameter
# its parameter is based on how many window we open in this for loop
# /k = cmd stays after terminated
# /c = cmd quits program completely
path = "start cmd.exe /K python index.py " + str(x)
threading.Thread(target=(os.system(path)))
time.sleep(5)
else:
print("Something is wrong, try again!")
# if user enters letters
except ValueError:
print("Please ONLY enter numbers")
I am guessing when I convert it into an .exe file, it did NOT load the dependent modules. My question is, how do I make it so it runs on a machine that doesn't have python installed?
NOTE: master.exe runs another python file index.py
Could it be that when I convert master.py into .exe it only loads dependencies that are written in the master.py and ignores index.py dependencies????
I am designing a game in which I require the input function to accept input without requiring the user to press the enter key. Also,once the user enters a single character,the variable in which the input is supposed to be stored must contain that character at once(as if the user had pressed the enter key after entering that character). The character entered must be displayed on screen only if it satisfies a given condition. Please enlighten me on how I can achieve this. Thank you.
NOTE:I am a beginner in Python, hence please do not delve into things that are way beyond my comprehension.My teacher has forbidden us from using classes for our project. I should be able to utilize the characters displayed on screen which I don't find possible using the solutions given for other similar questions.
After searching, I found a packaged named msvcrt, and it can be helpful in your situation.
For more information, please check: raw_input in python without pressing enter.
Following is a simple example(it works in Windows). You should open your command line and use python xxx.py to execute the following program.
import msvcrt
import sys
while True:
c = msvcrt.getch()
if c == b'\x03': # Ctrl-C
sys.exit()
#print(type(c)) # bytes
#convert from bytes to str
c = c.decode('utf-8')
if c =='a': #replace 'a' with whatever you want
print(c)
It takes the single character input from msvcrt.getch() and assign it to the variable c.
And it prints that character if its string format equals to 'a'.
If you want to exit the program, just press Ctrl-C.
EDIT:
From Python kbhit() problems, it seems msvcrt.getch cannot work in IDLE by nature. But there is a workaround (adjusted from: PyCharm: msvcrt.kbhit() and msvcrt.getch() not working?). If you are using Spyder, click Run -> Configuration per file -> Execute in an external system terminal. And when you later run the script file, a console window will jump up.
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.
This question already has an answer here:
Running Code in Sublime text 2 ( Mac OS X )
(1 answer)
Closed 7 years ago.
This is the first line of my program:
def main():
country = input('\nPlease enter the country> ')
I selected the python Build, and when I click Ctrl-B it compiles fine, and tells me how long it took. Why doesn't it print the line and ask me for input?
You have to call the main function/the notation you're liking looking for is
if __name__ == '__main__':
country = input('\nPlease enter the country> ')
I am assuming you haven't called the function main anywhere in your code. You can solve this by adding if __name__ == '__main__': main() at the end of your code.
you should be using sublime-repl.
to install it you need package control available here.
Then open sublime text.type ctrl-shift-p ,choose sublime-repl-python
this will run an interpreter in sublime text.
import the file where you defined the module(eg.test.py).
and then call test.main()
I have a simple Python program that asks yes or no question and I validate that input.
If I run this Python shell, it runs fine. If I enter invalid characters it loops back to top of while.
However, if I run this in the terminal window and try to enter an invalid character it errors as shown below.
endProgram = 0
while endProgram != 1:
userInput = input("Yes or No? ");
userInput = userInput.lower();
while userInput not in ['yes', 'no']:
print("Try again.")
break
endProgram = userInput == 'no'
Looks like your RPi is using Python 2; the input function does an eval there.
input in Python 3 is equivalent to raw_input in Python 2. (See PEP-3111)
Ideally, you should change your RPi interpreter to Python 3. Failing that, you can make it version-agnostic like so:
try:
input = raw_input
except NameError:
pass
I can clearly see in the interactive shell you working in python 3.2.3 (background). But I can not see the python version you're running from the command line (foreground).
On your raspberrypi, execute this command from the shell:
python --version
I am expecting to see python 2.x here, because the behaviour of input() differs between python 2 and python 3, in a way that would cause exactly the behaviour you have seen.
You might want to add a line like
#!/usr/bin/env python3
To the top of your .py file, and then chmod +x on it. Afterward you should be able to execute it directly (./guipy01.py) and the correct python interpreter will be selected automatically.