Hi guys ,

I have the following code :

x=int(input())

If I get a string from the user string ,list or a double , then I'd an exception .
Is there a way to get any input from any type , and store it within "x" , even though
I write "x=int(input())" ?

meaning is , if I get the string "onetwothree" into the variable 'x' , then
first I need to define 'x' as "onetwothree" , and then execute the

"x=int(input())

I've tried with 'type' :

def translate(someType):
    return (type(someType))

but it didn't work out . I'd appreciate any ideas on the matter


Regards,Ron

Recommended Answers

All 3 Replies

You have looked through Code snippets, haven't you? What you think of my snippet http://www.daniweb.com/software-development/python/code/364647? Not helpfull?

Your post indeed looks nice,however I must use

x = int(input())

since that's my assignment's orders .
I've tried using your proposals in the post , with Exceptions , but still didn't work.
Any idea how I can combine it with the demand "x = int(input())" ?

again,thanks
Ron

For some odd reason I remember seeing this very same question somewhere ...

# if the error happens in the outer int() function,
# then the input() value is lost
# recover it from error message e
# Python3 syntax

try:
    x = int(input("Enter a number: "))
except ValueError as e:
    print("You need to enter an integer number")
    print(e)
    print("You entered %s" % (str(e).split(':')[1]))

'''possible result ...
You need to enter an integer number
invalid literal for int() with base 10: '1.4'
You entered  '1.4'
'''

Wonder who the meshugene instructor is?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.