+ 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

12th Oct 2017, 3:32 AM
Caleb Hale
Caleb Hale - avatar
5 Antworten
+ 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).
12th Oct 2017, 5:35 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
Can you post your code
12th Oct 2017, 3:42 AM
ChaoticDawg
ChaoticDawg - avatar
+ 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)
12th Oct 2017, 4:09 AM
ChaoticDawg
ChaoticDawg - avatar
+ 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 ,
12th Oct 2017, 6:44 AM
Sura Wankam
+ 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))
12th Oct 2017, 7:00 AM
ChaoticDawg
ChaoticDawg - avatar