Convert any user input to int in Python

后端 未结 3 422
别跟我提以往
别跟我提以往 2021-01-29 06:12

I need to convert user input to int. Below is what I have written so far which is not working out. It only accepts int. the end goal is to

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-29 06:41

    The input are in string format so first you need to convert it into float and then from float to int

    You can do

    i = input("Enter any value: ")
    
    print(int(float(i)))
    
    

    or you can do

    i = float(input("Enter any value: "))
    
    print(int(i))
    

提交回复
热议问题