Python Forum
Getting type from input() function in Python 3.0
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting type from input() function in Python 3.0
#2
(Jul-23-2019, 02:48 AM)leodavinci1990 Wrote: I want to enter 1 and return a data type class <int> for example or the same with list, float, dict

It is hard to tell a computer what you mean when typing 1. It may be a char (in case of python -- a string of len 1),
integer value (as you want), and float value too (1 belongs to the set of real number); or even something else, we can
imagine that it is a complex number 1+i0. However, input is used to enter some characters from keyboard, so it looks natural that input returns a string.

However, you can override its default behavior, e.g.

_input = input

def input(q):
    x = _input(q)
    # if x consist only of digits, it is likely should be integer,
    # lets convert it to integer and return it
    # if x consist of digits and has e, E, +, -, or dot, that might be a float (this should be clarified)
    # lets convert it to float and return it
    # and so on...
    
    # if no condition were satisfied return result as is.
    return x
Reply


Messages In This Thread
RE: Getting type from input() function in Python 3.0 - by scidam - Jul-23-2019, 11:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 1,128 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  determine parameter type in definition function akbarza 1 625 Aug-24-2023, 01:46 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,147 Dec-25-2022, 03:00 PM
Last Post: askfriends
  i want to use type= as a function/method keyword argument Skaperen 9 1,976 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  how to make sure an input is from the wanted type astral_travel 16 2,547 Oct-31-2022, 04:11 PM
Last Post: astral_travel
  Showing an empty chart, then input data via function kgall89 0 999 Jun-02-2022, 01:53 AM
Last Post: kgall89
  match type with value in csv parsing function vinci 2 1,347 Jan-21-2022, 12:19 PM
Last Post: Larz60+
  input function question barryjo 12 2,805 Jan-18-2022, 12:11 AM
Last Post: barryjo
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,237 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  Problem with input after function luilong 10 4,176 Dec-04-2021, 12:16 AM
Last Post: luilong

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020