简体   繁体   中英

Python user input and mathematical operation error

>>> g = input("Enter a number")
Enter a number43
>>> g + 2
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    g + 2
TypeError: Can't convert 'int' object to str implicitly

The program wont let me use the input variable in mathematical operations, like g + 2

In python 3, input() returns a string type even if you input a number. Thus, you're trying to add an integer with a string

You need to convert it to an integer type by using int() :

g = int(input("Enter a number"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM