x=x-1 does not work after input() -- string has to be converted to a number

Asked by dustin

I am having trouble with simple math in a while loop. The program refuses to perform the subtraction and continue the loop. Here is the response:

[error] Error message: Traceback (most recent call last):
 File "C:\Users\Kirby\AppData\Local\Temp\sikuli-tmp3822756621507804793.py", line 7, in
 x=x-1
TypeError: unsupported operand type(s) for -: 'unicode' and 'int'

x = input("How many times to type Hello?:")
if (x > 0):
    type("Hello")
    x=x-1

x=x-1 is the problem. Tried many varriations x=x-- ect so far nothing works.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
dustin
Solved:
Last query:
Last reply:
Revision history for this message
dustin (d-macdermott) said :
#1

sorry quick code fix I was trying varriations of if. Still dosn't work with while command.

x = input("How many times to type Hello?:")
while (x > 0):
    type("Hello")
    x=x-1

x=x-1 is the problem. Tried many varriations x=x-- ect so far nothing works.

Revision history for this message
dustin (d-macdermott) said :
#2

ok never mind mannaged to figure it out after all. here is the corrected code.

x = input("How many times to type Hello?:")
while ( x > 0 ):
    type( "Hello" )
    x = int(x)-1

Revision history for this message
RaiMan (raimund-hocke) said :
#3

not really:

since the input return is a string, this should be the solution:

x = int( input("How many times to type Hello? (Give me a number)") )
while ( x > 0 ):
    type( "Hello" )
    x -= 1

This would crash if at input you give something, that cannot be converted to an integer number.