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
print repr(text)to see what exactly is in the string\ris a carriage return. What operating system are you running on? and are you running the file via VScode, or via CLI/Termina/CMD. Short answer is that I am guessing you are using Windows.\r\nis standard windows EOL, whereas unix uses\n. raw_input is stripping the the newline, but the carriage return., and I am not sure whyrstrip()to remove the \r from the end of the line.