How do I convert my string to an integer to multiply it by a string?

Viewed 50
import turtle

var=1
t=turtle
print('Command here:')
while True:
    cmd=input()
        var=input()
        if cmd=='f':
                t.fd(1)
        elif cmd=='b':
                t.bk(1)
        elif cmd=='u':
                t.pu()
        elif cmd=='d':
                t.pd()
        elif cmd=='r':
                t.rt(1)
        elif cmd=='l':
                t.lt(1)
    elif cmd=='':
                t.fd(0)
        elif cmd.isnumeric():
                if var==0:
                        var+cmd
                else:
                        var*cmd
        elif cmd=='x':
                break
        else:
                print('Unknown command, try a different command')

That's my code and I am trying to multiply it by a string. Whenever I run the code, I get this error:

I've tried doing int(cmd)*var, and also var=int(input()) but nothing seems to work. I can't understand the problem. I just need to be guided in the right direction as I've been stuck on this part for ages.

  File "as2.py", line 27, in <module>
    var*cmd
TypeError: can't multiply sequence by non-int of type 'str'```
1 Answers

This worked for me:

cmd=input()
var=input()
if cmd.isnumeric():
    print(int(var)*int(cmd))

5
5
25

Try using int() on both parameters.

input() returns a string representation so both variables must have their values cast to int first.

Related