0

does anyone know why this code is unable to work properly

X=raw_input("enter a decimal") if X<36 and (X<>int(X)): print (X!+36%X)*2-1 else: print "try again"

8th Aug 2017, 2:54 AM
Courage
3 odpowiedzi
+ 1
Sorry, I am newbie in Python. 1) Why don't you use input()? raw_input() was renamed to input() http://docs.python.org/dev/py3k/whatsnew/3.0.html 2) What do you want do with "X<>int(X)" ? If you wanna check the variable type, you can use "type (X) == int" 3) Here the syntax is wrong "(X! + 36...)" Maybe do you want something like that? try: X = float(input("enter a decimal")) if X < 36 : X = 1 if X == 0 else X print ((X + 36 % X)*2-1) else: raise except: print ("try again")
8th Aug 2017, 3:46 AM
Oscar Albornoz
Oscar Albornoz - avatar
+ 1
There are several issues with this code. You're using method's and syntax for a 2.x version of python and the playground uses 3.x. This line: X=raw_input("enter a decimal") needs to just use input instead as @Oscar has noted: X = input("enter a decimal") The next line: if X<36 and (X<>int(X)): has several issues as well. First you're comparing X to 36. X is a string type and 36 is an integer, so you'll get a TypeError for this comparison. You're asking for a decimal in your input statement, so you may want to to convert X to a float and then make the comparison. However, the next part won't work either. Firstly, again, X is of type string. Next you should use != as the preferred way for checking if not equal as using <> may require an import or is no longer valid (not really sure). Anyway, then there would be the issue of trying to convert a string that contains a decimal and not an int into an int. This will result in a ValueError as the method is expecting to see a string integer being passed to it. You can however, convert it to a float first and then convert it to an int. if float(X) < 36 and (float(X) != int(float(X))): The next line: print (X!+36%X)*2-1 Again, has a few issues. First being that it is not properly indented. Next, in Python 3.x, the print function needs to have its arguments surrounded in parentheses. Next, X! is not proper syntax at all. I'm guessing that you want the factorial of X here. In order to do this you'll need to first import the math module. Then you can use its factorial function. You'll need to convert X to a float and then to an int as before though. Finally, you again need to convert X to a float or int for the modulus. print((math.factorial(int(float(X))) + 36 % float(X)) * 2 - 1) Lastly the print statement for the else also needs to be indented and have the argument in parentheses. else: print ("try again")
8th Aug 2017, 4:55 AM
ChaoticDawg
ChaoticDawg - avatar
0
Please help me
8th Aug 2017, 2:54 AM
Courage