Python problem with input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python problem with input
# 1  
Old 08-15-2017
Python problem with input

Hello everyone,


I have just started python, and there is something that escapes me. I don't understand why my little script doesn't work.

When I do, it work:
Code:
my_string = "bonjour"

if len(my_string) < "6":
        print(my_string + " < 6")
else:
        print(my_string + " > 6")

But when I do, It doesn't work, why ?!
Code:
my_string = input(": ")
my_string = str(my_string)

if len(my_string) < "6":
        print(my_string + " < 6")
else:
        print(my_string + " > 6")

output:
Code:
Traceback (most recent call last):
  File "./test.py", line 6, in <module>
    my_string = input(" :")
  File "<string>", line 1, in <module>
NameError: name 'hello' is not defined

Thanks by advance..
# 2  
Old 08-15-2017
Quote:
Originally Posted by Arnaudh78
...
...
But when I do, It doesn't work, why ?!
Code:
my_string = input(": ")
my_string = str(my_string)

if len(my_string) < "6":
        print(my_string + " < 6")
else:
        print(my_string + " > 6")

output:
Code:
Traceback (most recent call last):
  File "./test.py", line 6, in <module>
    my_string = input(" :")
  File "<string>", line 1, in <module>
NameError: name 'hello' is not defined

...
Please mention the Python version you're using.
Since the difference between your two programs is the "input()" function, it should be clear that the "input()" function is the problem.

From your error message, I assume you are using Python 2.
In Python 2, "input()" function tries to evaluate the expression you enter at the prompt. Check the documentation here:
25.4. 2to3 - Automated Python 2 to 3 code translation — Python 2.7.13 documentation

So, when you enter "hello" at the prompt, Python tries to evaluate it and throws an error since it is an undefined identifier.

Code:
$
$ cat -n test.py
     1  my_string = input(": ")
     2  my_string = str(my_string)
     3
     4  if len(my_string) < "6":
     5      print(my_string + " < 6")
     6  else:
     7      print(my_string + " > 6")
     8
$
$ python --version
Python 2.5.1
$
$ python ./test.py
: hello
Traceback (most recent call last):
  File "./test.py", line 1, in <module>
    my_string = input(": ")
  File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
$
$

If you really want to use the "input()" function, enter an expression that could be evaluated by "eval()" and you will not see the error message.

Code:
$
$ python ./test.py
: 2 + 3
5 < 6
$
$

In the above execution, I entered "2 + 3" which was evaluated by "eval()" to obtain 5. That was converted to string and assigned to "my_string" and then the comparison works.

In case you want to enter a string value like "hello", use "raw_input()" function in Python 2.
Check the documentation here:
2. Built-in Functions — Python 2.7.13 documentation

Code:
$
$ cat -n test.py
     1  my_string = raw_input(": ")
     2  my_string = str(my_string)
     3
     4  if len(my_string) < "6":
     5      print(my_string + " < 6")
     6  else:
     7      print(my_string + " > 6")
     8
$
$ python --version
Python 2.5.1
$
$ python ./test.py
: hello
hello < 6
$
$

This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 08-15-2017
Thanks for your reply, my version is Python 2.7.6,
Code:
my_string = raw_input(": ")

It work for me Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

creating separate output file for each input file in python

Experts, Need your help for this. Please support My motive is to create seperate output file for each Input Files(File 1 and File2) in another folder say(/tmp/finaloutput) Input files File 1(1.1.1.1.csv) a,b,c 43,17104773,3 45,17104234,4 File 2(2.2.2.2.csv) a,b,c 43,17104773,1... (2 Replies)
Discussion started by: as7951
2 Replies

2. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

If condition after raw input in python

Hi, I have a python script which is completely interactive. I have almost 10+ raw input statements like below which are collected from user one by one. req_number = raw_input("Please enter request number or Q to Quit: ") if (req_number in ): sys.exit() Is it possible for a more... (6 Replies)
Discussion started by: ctrld
6 Replies

4. Shell Programming and Scripting

Kbd input processing with python

Hi, I'm new to python, and I work on a little program fpr my rapsberry pi. This scenario here is a little Midi sysex app for my Roland D-50 Synthesizer. With an usb attached number keypad I can type numbers from 0 to 9 and the referenced Midi sysex file would be send out to my Roland D-50... (1 Reply)
Discussion started by: sdohn
1 Replies

5. Shell Programming and Scripting

Python SSH problem

Hello geeks, Am trying to write a custom plugin using python but it seems python is not handling the SSH part well, find below for the code: #!/usr/bin/python import os, sys host = sys.argv pdpactgsm=os.popen("ssh -l GbeAdi $host "pdc_kpi.pl" | grep -A 4 sgsn_g | awk 'NR == 5 ' |... (1 Reply)
Discussion started by: infinitydon
1 Replies

6. Shell Programming and Scripting

Reducing the decimal points of numbers (3d coordinates) in a file; how to input data to e.g. Python

I have a file full of coordinates of the form: 37.68899917602539 58.07500076293945 57.79100036621094 The numbers don't always have the same number of decimal points. I need to reduce the decimal points of all the numbers (there are 128 rows of 3 numbers) to 2. I have tried to do this... (2 Replies)
Discussion started by: crunchgargoyle
2 Replies

7. Shell Programming and Scripting

Problem with python script

I have a txt file of 8GB with same type of messages in it. for eg: MTQ_BQUOTE, Length: 40, Timestamp: 4:00:52.064 MsgKey: symbol: XXX | reportingExchange: 11 Symbol: XXX, hash 004C5746 QS Symbol: XXX, market 1 Security Type: EQUAL (1) Symbol Type: Equity.Share.Single.None Session: XX_XXX (0)... (9 Replies)
Discussion started by: Vijeta Laad
9 Replies

8. Shell Programming and Scripting

giving input to a python wch has been called frm shell script....urgent

i'm calling a python script from shell script. the python needs Y as an input everytime. how can i giv it thru shell script. I tried below code for arg in `cat erd_gen_list.lst` do generateErdSql.py -S $arg << Y done This is giving me err : `<<' unmatched pls help. (1 Reply)
Discussion started by: vini
1 Replies

9. UNIX for Dummies Questions & Answers

problem with python on mac

Hi, I'm sure there's a very easy way to fix this but i'm a newbie so I'm posting this. The problem is that whenever I run python on the terminal I can no longer use the left and right arrows to move left or right, or the up/down arrows to call commands used before. Whenever I click an arrow I get... (0 Replies)
Discussion started by: thefloydpink
0 Replies

10. Shell Programming and Scripting

Python tkinter grid problem?

There seems to be some problem with the grid geometry manager, or maybe its just my syntax thats incorrect. Im using Python 2.3.4. My problem is that even when I use the sticky='news' option in any grid method, it just doesnt work period. For example, the following code gives be a window with a... (0 Replies)
Discussion started by: SRCoder!
0 Replies
Login or Register to Ask a Question