I get this error from python when I try to run my program does anyone know how to fix it.
ops.append(i ".)" names[i] "'s Living Quarters\n")
TypeError: unsupported operand type(s) for : 'int' and 'str'
ops is a array for choices.
names is a array with names to be made in to the ops array with a number for printing. i is a increasing number for the choice number. sorry if there have been other questions like this, I couldn't find a solutionCodePudding user response:
You'll need to convert your integer to a string before you can concatenate it. You can do this with str(i)
.
Or you can accomplish your append line with f-strings, like so:
ops.append(f"{i}.) {names[i]}'s Living Quarters\n")
CodePudding user response:
You can use an integer in a string by either converting the integer to a string using
str(variable)
, or by formatting it in the string using F-strings.
String formatting example:
stringName = f"Number: {integer_variable}"
Which can also be used for other variable types and is a bit more readable than concatenating a ton of variables to strings using
CodePudding user response:
There's lots of fun ways to format strings in Python. I tend to prefer string.format
just because of the flexibility.
ops = "{}{}.) {}'s Living Quarters\n".format(ops, i, names[i])
Ideally, you'd include the formatting for ops in there as well, but since I didn't have the code you used to generate it , I just showed you the closest I could.
CodePudding user response:
ops.append(str(i) ".)" str(names[i]) "'s Living Quarters\n")
Should work!
str(VARIABLE)
converts the VARIABLE
into STR
(String)