I have a few python files that take input as two ints separated by spaces, and return an int. (My class requires this.) I'm having an issue where an extra "D" appears along with my output, after I hit Ctrl-D to end the input. It's running the programs correctly, though - the output is correct.
Here's what I'm seeing:
$ python gcd_euclid.py
144 100
4D
$ python pc_1_ucsd.py
3 4
7D
Oddly...this wasn't happening yesterday, and I'm not sure if I changed anything. Does anyone have any ideas why this is happening and how to fix it?
Edit: Here is the snippet that the course provided for reading the input. I hadn't used sys.stdin before this week.
import sys
input = sys.stdin.read()
tokens = input.split()
a = int(tokens[0])
b = int(tokens[1])
print(a + b)
(they chose to use the input keyword as a variable, not me!)
I suspect it is old keyboard echo (where did the line feed go?) Try feeding shell input using something like:
command-and-args <<!EOF
input lines
!EOF
Related
I have this part of python code that should read input values from the screen.
When it runs, it keeps running forever. I tried CTRL+D as per the search results, but it does not work. This code is given and it works when I submit the code to an online grader but does not work on my windows machine.
if __name__ == "__main__":
data = list(map(int, sys.stdin.read().split()))
n, capacity = data[0:2]
values = data[2:(2 * n + 2):2]
weights = data[3:(2 * n + 2):2]
opt_value = get_optimal_value(capacity, weights, values)
print("{:.10f}".format(opt_value))
Sample input should be:
3 2
20 50
12 12
51 51
On Windows you should use Ctrl-Z instead of Ctrl-D. Also, this should be the first character of a newline, and there should be a newline (enter) after the Ctrl-Z as well. There may be other characters after the Ctrl-Z, which will be ignored.
So, the shortest sequence to force an EOL on Windows is: Enter, Ctrl-Z, Enter.
I know this from experience and did found some answers or comments stating the same, but I couldn't find any official documentation where this is explained. If I find it, I will add it to this answer.
(please leave a comment if you know a reliable source for this behaviour.)
I"m trying to store 2 multi line inputs in 2 variables. However, std input methods keep getting hung up. I cannot change input format, but I can always expect 2 strings will be provided together. There is a new line character at the end of the first block and a new line character at the end of the second block.
I have tried several solutions from previous posts on how to accept multi line input, but none work for this case:
Store multi-line input into a String (Python)
When I try to use this code:
y input is accepted but x input is never accepted. I think the compiler is misinterpreting a line break somehow.
Any suggestions would be really appreciated.
y = input().strip()
x = input().strip()
TGGGAGGAGCAGTGATAATGCTACCTTGCTCGTGCCCCTTTAATGCCGGTGTCATCGCCTTAATGGGGTTCACAAGCAGTTACGGGGGGTCAAGTAATCATCGCGTTCGTCTTAGACGTGTCAGAGAACTAGTTTTGGAATCATTAACGACCTGCAATACTAGGTGCCAGCATAGGGTCTTTCGGAAGCACAACGTTGGAAGGCATCGCTTAGTGTAACCTAGTTACGGGAAGGCTACAGGTGCGGATGGTGGGGCCCAGCTGGGACTCAATCCAGGGACATCGGACTTTCGTTGGGGTTGAGCGGTCGCGAGGATTCGCAAGAGGGCGCCTTACAATGTTACCGTTTAGATTTACGGCCATTCCGACTTTGCAATTATTACCTTGAGCGATGCCGGCCATGCCGCGACATTATCAGTGATGTGTTCTCTCTCGGCTTGGTCGTTCAAACACGGGGCGTCATAGCTGAGTAGCCAAGGCAAACCAATTACCTACGTTCTGACCTGGCTGAATCTTGTGGAGCACCGGATCCAAGCAGTCGTGCCGGAGATTGTAGGCCAGCTTGTCATTCTGGATTGCGTTCCGCCCGATGTGAGCTTGTTCATACACCGAGTACGGGCTGCATTGGATCGTTCTATACGACAACGCTTCAGATCTCGAGTGCGTGGATCGAAGAAAGCGGAAGTCCGTTCGCGGACGCTCACAGCTGGTTGCTGCCGGCACCAACATGAACCGATCACCTAGCGCTTATAGTAAGCGGATATATCTTAGTATTAACCTTTCATTCCGGGCGGCACCTGAATGGGCAGTCTCGATTGATTAAGATCCCTTACTCTTCGAACTCGCGCGGACACGTCGTGCGCATCAATGGCAGTTATCTCGTATTAATACATACGCGTGATCAGCGCTATAGGGTATTTTTAGTTTTGGTCAACTCCGCAGTCACTGTGGATTGAATTGAGCATGCGGGCGAAGATCTGCTTTTCATCGCCTCTAACCAA
TATGGAGGACCAATGGTAGATGATACCGTTGCTCGGGGCGCTTTAAAGCCGGAGTAATGCGCCTATGAATGTGGGTTCACAGAGGAGTTCCCGGGGGGTCAACATCATCATGCGAGTGTCGTCTTAAAAGTGGCAGAATAACTGGTTTCTATGTATATCATTGAGCACCATCAATACGAGGTGTCAGCATGAGGCTCGTTCGGATGCCCGGCCTTGGAAGGCATCTGAGTTAGTATAACACTAGTTACGCGAAGGCTACAGGTGCGTGATCGTGCGGCCCAGTTGGGACTCAATTGAGGGACACGCGGACTTTCGTCGGGCTGTCAGAGGCCGGTCGGGGGAATGCGCAGGTTGTGCGGCACATACAATGTTATCGTTTCAGAATTTTATCGGCCATATCCGACTTTGCAATTATATTCCTTGCAGCGATGCCGGGGGAGCCGCGTACATGCATCAGTGTATGTATGCTCACTCGGCTATGTCGGTTCATAACCTGGCGCATATTAGCTGAGTAGACAAGGACTAAACCAATTAAACTACGTTCTGACCTCGCTATAGTATGTGAGTGAGTCACCGGATCCGAGCAGTTCGGGCCGCAGATTGGAGGCCAGCTTGTCATACTGGGTTGACCGTTTCGCCCGATGGGAGCTTGGTATCATACATCGAGTTACGTGGCTGCATTGTGTATCGTTCTGTTACGTACAACGCCTTCAAGGTCCCGAGTGCGAGGGTTCCCAGAAAAGCTGGAAGCGCAGTTCGTGAACTGCTCACAGCTGGTGGCTGCCGGCACCAACATGCACTTCGACTCACCTACCCAGCTAAATGTAAGCGCATATCTCTTAGTATATAACCTTTACATATCCGGGCGACGTACAGTAAAGAAGCAGGCTCGATGTCGTAGAGTTACCCTTACTACACTCGCAAATCGCGCGGACACGGTATGTACGCATTGAATCGACAGTTCTCTCGTATTTAGTACATACGCGTGATCAGCTGCTATAGAGTAATTCTAGCTTTGAGTGAACACCTCAGTGATGGCTGGATTGTAACTGAGCAACGCGGTCTGAGCGAACGGTTTTTGCATCGCGCTCTAACCAGG
I figured out that the problem was IDE specific. VS Code cannot accept large string input from console. No problems when using PyCharm.
in my class we are studying python 2.7. I am using vscode to test the exercises.
exercise 1: read user input and print the length. If the user write
exit the program finish.
My code is follow:
myexit=False
while (myexit!=True):
#read user input
txt=raw_input("write a string or write exit to go out: ")
#print the user input string
print txt
if (str(txt)=='exit'):
myexit=True#exit from while
else:
print len(txt) #print the string length
print "finish"
when i test the code i get always the length of the string +1
example: if i write foo the output is 4 and no 3. When i write exit i
don't go out from the while and the output is 5.
Where i wrong ?
I have missed a module?
Thanks for your help
I am not sure exactly why this is happening, and I don't have access to a windows machine to test/verify but based on the comments above, it appears that on the version of python you are using that raw_input is only stripping the newline(\n) and not the carriage return(\r). Windows uses \r\n while unix uses \n. When raw input returns the \r is still on the string, hence the extra char. A useful debugging technique at the cli is to use the function repr() on the value to see exactly how it is represented. This is helpful to locate any stray control or invisible chars in strings.
The function rstrip() will remove all whitespace from the right side of the string, which in this case should safely remove the stray \r. It should also be safe if this code is running on a *nix like system as rstrip() will only remove the whitespace if it is present. You can also specify a set of char to strip, so if you would would like to be pedantic, you could use rstrip("\r").
txt=raw_input("write a string or write exit to go out: ").rstrip("\r")
Should fix the issue while still maintaining compatibility on different versions.
I have a long list of numbers that I would like to input into my code through a raw_input. It includes numbers that are spaced out through SPACES and ENTER/RETURN. The list looks like this . When I try to use the function raw_input, and copy paste the long list of numbers, my variable only retains the first row of numbers. This is my code so far:
def main(*arg):
for i in arg:
print arg
if __name__ == "__main__": main(raw_input("The large array of numbers"))
How can I make my code continue to read the rest of the numbers?
Or if that's not possible, can I make my code acknowledge the ENTER in any way?
P.s. While this is a project euler problem I don't want code that answers the project euler question, or a suggestion to hard code the numbers in. Just suggestions for inputting the numbers into my code.
If I understood your question correctly, I think this code should work (assuming it's in python 2.7):
sentinel = '' # ends when this string is seen
rawinputtext = ''
for line in iter(raw_input, sentinel):
rawinputtext += line + '\n' #or delete \n if you want it all in a single line
print rawinputtext
(code taken from: Raw input across multiple lines in Python )
PS: or even better, you can do the same in just one line!
rawinputtext = '\n'.join(iter(raw_input, '') #replace '\n' for '' if you want the input in one single line
(code taken from: Input a multiline string in python )
I think what you are actually looking for is to directly read from stdin via sys.stdin. But you need to accept the fact that there should be a mechanism to stop accepting any data from stdin, which in this case is feasible by passing an EOF character. An EOF character is passed via the key combination [CNTRL]+d
>>> data=''.join(sys.stdin)
Hello
World
as
a
single stream
>>> print data
Hello
World
as
a
single stream
So I'm writing a differential calculator program in python 2.4 (I know it's out of date, it's a school assignment and our sysadmin doesn't believe in updating anything) that accepts a user input in prefix notation (i.e. input = [+ - * x^2 2x 3x^2 x], equivalent to x^2 + 2x - 3x^2 * x) and calculates the differential.
I'm trying to find a way to read the command line user input and put the mathematical operators into a queue, but I can't figure it out! apparently, the X=input() and x=raw_input() commands aren't working, and I can find literally 0 documentation on how to read user input in python 2.4. My question is: How do I read in user input in python 2.4, and how do I put that input into a queue? Here is what I am trying:
1 formula = input("Enter Formula:")
2
3 operatorQueue=[]
4
5 int i = len(formula)
6
7 for x in formula:
8 if formula[x] == '*', '+', '-', '/':
9 operatorQueue.append(formula[x])
0
11 print "operator A:", operatorQueue.pop(0)
12
Which is not working (I keep getting errors like "print: command not found" and "formula:command not found")
Any help would be appreciated
#miku already answered with this being your initial problem, but I thought I would add some more.
The "sh-bang" line is required by command line scripts so that the proper process is used to interpret the language, whether it be bash, perl, python,etc. So in your case you would need: /usr/bin/env python
That being said, once you get it running you are going to hit a few other issues. raw_input should be used instead of input, because it will give you back a raw string. input is going to try and eval your string which will mostly likely give you problems.
You may need to review python syntax a bit more. Assignments in python don't require that you declare the variable type: int a = 1. It is dynamic and the compiler will handle it for you.
Also, you will need to review how to do your if elif else tests to properly handle the cases of your formula. That too wont work doing it all on one line with multiple params.
If you're on a unix-ish platform, put a
#!/usr/bin/env python
on top of your program. The shell does not seem to recognize that you are running a python script.