-1

So I have been making a program in Visual Studio using the Python 3.6 (64 bit) and for some reason whenever I try to add a number to a user input it comes up as a error. e.g.:

while X == 0:
    print('Enter Input')
    Y = input('>')
    Y = Y + 180
    print (Y)

and when I run this code it comes up with a error saying I have to convert it to STR, not INT. But when I try to run it with the command as seen below:

while X == 0:
   print('Enter Input')
   Y = input('>')
   int(Y)
   Y = Y + 180
   print (Y)

When I run this code the same error appears, switching out the 'int' bit of code with 'STR' gets the exact same error despite the fact that the debugger lists both the variables as the same encoding method (Str) am I just retarded? please help.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80

3 Answers3

0

You are close on your second attempt

while X == 0:
   print('Enter Input')
   Y = input('>')
   Y = int(Y) + 180
   print (Y)

When doing arithmetic you need two like types. Therefore you must cast Y as an int (it is by default a string since it was inputted from the keyboard).

As commented above if you're expecting an int for input you can do

Y = int(input('>'))

This will try and convert whatever you input into an int.

ltd9938
  • 1,444
  • 1
  • 15
  • 29
0

you are not assigning int(Y), The correct form should be Y = int(Y)

But i prefer to simplify it in one statement Y = int(input('>'))

Vipin Mohan
  • 1,631
  • 1
  • 12
  • 22
0

In your code what you are doing is -

Y = input('>')
int(Y) # typecasted but did not assign it back to Y
Y = Y + 180

Change it to -

Y = int(Y)

And it should work as expected. You see this because input always returns a string. Also, a better thing to do would be -

Y = int(input('>'))
Sushant
  • 3,499
  • 3
  • 17
  • 34