when I try to change a float data type from user input I get this error # ValueError: invalid literal for int() with base 10:

when I try to change a float data type from user input I get this error # ValueError: invalid literal for int() with base 10:

Abdulla-Qaladzay
Collaborator Collaborator
707 Views
1 Reply
Message 1 of 2

when I try to change a float data type from user input I get this error # ValueError: invalid literal for int() with base 10:

Abdulla-Qaladzay
Collaborator
Collaborator

this is how I solve it 

 

if I excite this line :

 

 

 

 

val = int(input("wsp?!"))

 

 

 

 

 and in-put a float value  I get the error that I mentioned in the title of the post

 

so 

this is how I got rid of it by the help of eval()  command:

 

 

val = int(eval(input("wsp?!")))

 

 

 

 

This error is not only happen in Maya 

I tried it with almost all newer versions of python than 3.7.7 in all the versions I got the error and the solution worked in all of them 

With python 2 that was OK I didn't get the error

 

I am not sure that could be something that I misunderstood cause I don't have much experience with python

so I am sorry if that it the case

🤍

 

0 Likes
Accepted solutions (1)
708 Views
1 Reply
Reply (1)
Message 2 of 2

e_honda
Enthusiast
Enthusiast
Accepted solution

Hi.

 

If I do not misunderstood the problem, I believe that the data type from user input is a string, not a float.

As so, you cannot convert a "float string" to "int" directly.

You should do a string -> float -> int conversion like below.

val = int(float(input("wsp?!")))

The eval command will act like the float conversion, but could be a security vulnerability because it will evaluate any code string.

 

Probably more explanations could be found in stackoverflow threads like those below.

https://stackoverflow.com/a/27048725

https://stackoverflow.com/a/26330367

 

Hope it helps.