+ 4
Type confusion: the code playground thinks an integer is a string?
in a project of mine i have "numselect = x" and x is an integer, so numselect should be an integer. later i have tried "numselect = numselect - 1" and "numselect -= 1" but neither work. I have tried adding "int(numselect)" to make sure it is an integer as well, but the program gives the error of incompatible types: int and str. 1 should be considered an integer by the program, is it not? what's wrong? https://code.sololearn.com/csHSAKTmbnpb/?ref=app
5 Réponses
+ 5
input() always returns a string, so you have to convert.
Actually, you might go about it like this:
x = int(input("enter (whole positive) number:"))
Then x will be already returned as an integer (provided it actuall will be a valid number, of course).
+ 3
Can you post your code
+ 1
The problem is that when you use int(numselect) you're not setting it back to the variable so the value returned is lost.
print ("welcome to the factorial calculator!")
numselect = int(input("enter (whole positive) number:"))
solution = 1
while not numselect == 1:
solution = solution * numselect
numselect -= 1
print ("The factorial of" + x + "is" + solution)
+ 1
i get an error when i don't use "commas" in the line print("the factorial of + x + "is"+solution") . i think you have to change + to ,
+ 1
@Sura the + will still work, you just need to convert them to a string first.
print("the factorial of " + str(x) + " is "+ str(solution))