This question already has answers here:
Arrow keys not working while entering data for input() function
(2 answers)
Closed 7 months ago.
Problem :
Previously I wrote all of my python code on a Windows 10 machine, but more recently I've been trying to move over to MacOS. Everything is working just fine, with one exception. I'm writing a program that uses the python 'input' function and when pressing ↑ on MacOS it prints '^[[a' but on Windows 10 it prints the previous input. Is there get the previous input with the up arrow on Mac?
Example Code :
while True:
string = input()
print(string)
On Windows hitting ↑ will print the previous input but on Mac '^[[a' will print
Use readline module to provide reading and writing of history files. More info on that in the docs
import readline
while True:
string = input()
print(string)
Related
This question already has answers here:
Is there a "not equal" operator in Python?
(10 answers)
Closed 1 year ago.
I am trying to check is osys variable is not equal to 'Linux'. The output of platform.system() on my machine is 'Linux'. This then gets assigned to the variable of osys.
def getos():
osys=platform.system()
if osys is not "'Linux'":
print(color.lightred + "This program only runs on Linux operating systems.")
time.sleep(2)
quit()
getos()
I am using this code to check if osys is 'Linux', and if not the program will close because the program only works on linux. Instead when I run this code I always get the output string of This program only runs on Linux operating systems instead of the code just continuing. Does anyone know how to fix this.
Try this instead you dont need quotes within the quotes.
if osys != "Linux":
This question already has answers here:
Can't send input to running program in Sublime Text
(5 answers)
How to Run Python Code on SublimeREPL
(4 answers)
Closed 1 year ago.
I've done everything to setup Python in Sublime Text 3.
When I execute my code, it displays the 1st line and then nothing else.
I can type after the 1st line but nothing happens.
This happens on every code I execute but i will provide my code and explain what it does
(code is on Croatian so dont mind the text)
Python version: 3.9.2
The code:
print('\n')
height = int(input("Unesi svoju visinu: "))
width = int(input("Unesi svoju debljinu: "))
BMI = width / (he/100)**2
if BMI<=18.4:
print("Ti se moraš udebljati!")
elif BMI<=24.9:
print("Dobar si!")
elif BMI<=29.9:
print("Malo se trebaš smršaviti!")
elif BMI<=34.9:
print("Trebaš se smršaviti!")
elif BMI<=39.9:
print("Trebaš se puno smršaviti!")
else:
print("Moraš odmah na treniranje!")
What the code does: It goes through the first input, when I type in the console to enter the input and I press enter, the code just stops executing.
VIDEO OF PROBLEM: https://youtu.be/ETsFJjw8O7s
A quick Google search leads you to this
discussion on the Sublime forum from 2018.
Sublime Text does not support inputting data into a program. You can up vote this feature request on: Currently you can try to install the package https://packagecontrol.io/packages/SublimeREPL and use it to run you program from a Sublime Text view. You you can try out the VSCode editor: https://github.com/Microsoft/vscode, which is pretty similar with Sublime Text.
I also cannot get user input to work, so presumably Sublime Text still does not support user input.
This question already has answers here:
Python, writing multi line code in IDLE
(7 answers)
Copy-paste into Python interactive interpreter and indentation
(9 answers)
Copying and pasting code into the Python interpreter
(11 answers)
Closed 2 years ago.
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.
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.
This question already has answers here:
How to rewrite output in terminal
(4 answers)
Closed 2 years ago.
Let's assume there is a code which is using with anaconda prompt. While the program flowing, every second is printing to screen. However, here every second is printed the same or next line.
I want to print every second in the same place.
For example, print 30 and after 1 second later, delete 30 and print 29 to the same place.
How can I do that with python?
You can use \r to return to the start of line instead of moving the cursor to the next line (\n). Whether or not this works with your shell is an open question; e.g. IDLE occasionally has problems with this.
import time
for x in range(10):
print(x, end='\r')
time.sleep(1)
print() # to add a newline, finally
print('The end!')
After every second when you have to clear the screen you can use this :
import os
os.system('cls')
This question already has answers here:
Can't send input to running program in Sublime Text
(5 answers)
Closed 2 years ago.
I have python 3.7 installed and I have this code:
print("Enter your name:")
x = input()
print("Hello, " + x)
I was writing the name and press enter but the input is not over, it is still running and waiting for more inputs
Edit: the problem is that input is never ending, doesn't matter how many enters I press
Sublime text doesn't support inputting data. Read this : Sublime text input not working
That said, apparently SublimeREPL provides this sort of functionality, although I don’t use it myself so I don’t know much about it.