3 Replies - 165 Views - Last Post: 16 December 2019 - 01:01 PM Rate Topic: -----

#1 Splashsky   User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 354
  • Joined: 25-August 13

Input() getting cast to type type but not str?

Posted 10 December 2019 - 11:08 PM

As far as I'm aware, input() should always return a string value, but in my case it's returning as a "type". Why is this?

Code
cmd = str(input())

print("command was " + cmd)

if cmd == "list":
    print(listSchedule())
else:
    # Work on this later
    print("close")



Output
list
command was <type 'list'>
close


Is This A Good Question/Topic? 0
  • +

Replies To: Input() getting cast to type type but not str?

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 562
  • View blog
  • Posts: 1,798
  • Joined: 27-December 13

Re: Input() getting cast to type type but not str?

Posted 11 December 2019 - 12:07 AM

Running your code I get:
list
command was list
Traceback (most recent call last):
  File "C:/Python36/test2.py", line 6, in <module>
    print(listSchedule())
NameError: name 'listSchedule' is not defined
>>> 

'list' is a python keyword.
Used in this way it leaves the compiler (or other part of the system) waiting for arguments - on my system this leads to an OS warning upon closing.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2756
  • View blog
  • Posts: 4,416
  • Joined: 21-June 11

Re: Input() getting cast to type type but not str?

Posted 16 December 2019 - 12:28 PM

View PostSplashsky, on 11 December 2019 - 07:08 AM, said:

As far as I'm aware, input() should always return a string value


This is true in Python 3, but you seem to be using Python 2. In Python 2 `input()` evaluates the user input as an expression (as if passed to eval) and returns the result as whichever type it evaluated to. Since that's rarely what you want, you should use raw_input instead when using Python 2.

Quote

but in my case it's returning as a "type".


Because you entered list, which happens to be the name of a type. If you had entered hello instead, you'd have gotten an error message about nothing with that name being defined. And if you had entered "hello", you'd have gotten a string.
Was This Post Helpful? 1
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3040
  • View blog
  • Posts: 11,691
  • Joined: 03-December 12

Re: Input() getting cast to type type but not str?

Posted 16 December 2019 - 01:01 PM

I will just reiterate what sepp said. The only way that you would see this error, was if you were using py2. py3 does not behave this way.


For python2 you should be using raw_input()

def listSchedule():
  print("A list should be here")

cmd = raw_input()

print("command was " + cmd)

if cmd == "list":
    print(listSchedule())
else:
    # Work on this later
    print("close")

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1