What is the problem with this program? I'm trying to make a program that determines whether a number is a perfect cube and find the cube root. def cuberoot(): x = float(raw_input('Enter a number: ') y = range(1,x) for z in y if z*z*z == x print x + ' is a perfect cube.' print 'The cube root is ' + z elif z*z*z != x print x + ' is not a perfect cube.' It keeps saying "syntax error" with the y in the third line highlighted. What is the issue?
Range() can only take integers as arguments, but you're feeding in x, which you've forced to be a float.
I've changed "float(raw_input('Enter a number: ')" to "int(raw_input(...." but it still says "syntax error".
when i ran your program I got error on print x + ' is not a perfect cube.' on this line with messge "TypeError: unsupported operand type(s) for +: 'int' and 'str'. ". You have explicitly convert x variable to string by str(x) and then append to a String literal.
Yes, I forgot to mention that I had changed that to str(x) -- it still says "syntax error" with the "y" in " y = range(1,x)" highlighted.
have you ended the for , if and elif with colon? bcz i didn't see that in your code above. Please post the updated code , you are getting error in.
It's probably better to put eval(raw_input("Enter number here: ")) instead of using int. And why put a variable called Y, just make the the code say: for z in range(1,x+1) The range should be (1,x+1) in case the number inputed is 1. If course, you need to put colons after all your if, for, while, etc. in general And, your elif statement reacts each time, so you have to put it outside your for loop so that it checks after all values have been checked .
Two questions. One, why should it be range(1,x+1)? I guess I should have some sort of branch such that it prints "Invalid entry" if the user enters a 1. But I'm not sure how to do that. Also, I don't understand why the elif statement needs to be outside the for loop...? Anyway, I did everything you said, and it highlights the colon after the for statement and says "syntax error". I deleted that colon and ran it again -- this time it says "syntax error" with the colon after the if statement highlighted.
Code thus far, still not working: def cuberoot(): x = eval(raw_input('Enter a floating-point number: ') if x == 0: print '0 is not a perfect cube.' elif x < 0: for z in range(x,0): if z*z*z == x: print str(x) + 'is a perfect cube.' print 'The cube root of ' + str(x) + ' is ' + str(z) else: print str(x) + 'is not a perfect cube.' elif x > 0: for z in range(0,x): if z*z*z == x: print str(x) + 'is a perfect cube.' print 'The cube root of ' + str(x) + ' is ' + str(z) else: print str(x) + 'is not a perfect cube.' else: print 'Invalid entry -- perhaps you did not enter a floating point number, or perhaps you entered an imaginary number...?'
pls use a code pasting site: http://dpaste.com/763251/ you are missing a close paren in line 2 line 16 indentation is wrong if you only want it to print when it determines if it is or is not a perfect cube it needs more logic. you can stop a for loop with a break statement like this: http://dpaste.com/763256/ here is one way to do it: http://pastebin.com/JeH9kmEk here is another: http://dpaste.com/763265/
Thank you, that helped a lot! However, there is still one issue. This is what I have now: http://codepad.org/VQR9981M When I input 216, it says "216 is not a perfect cube" several times and then says "216 is a perfect cube." What I want the program to do is take every number in the range(0,x) and evaluate all of the numbers in that range until it reaches the cube root (and then prints "is a perfect cube") or until it has evaluated all the numbers in the range (and then prints "is not a perfect cube"). I thought that is what the for loop would do, but it isn't doing that.
Ah, this error took me a bit to figure out. Let's just take a look at the "if x>0" part of your code for now. Currently, you're telling it to run the variable z from 0 to x. If z is the square root, you're telling it to print that z is the square root and then break out of the loop. That's fine. But if z is not the square root, you're telling it to print that x is not a perfect cube, *and then break out of the loop*. Basically, your loop only runs once right now, because you're breaking out if z works, and you're breaking out if z doesn't. Here's how you fix your code: You only want to say that x is not a perfect cube if it reaches the end of the for loop without breaking out. Soooo... 1. In the part where you're checking whether z IS the square root, change "break" to "return". You don't want to just break out of the for loop, you want to be done with the entire program. 2. Instead of having an "else" clause, delete the "else" and then make the print line align with the for loop line. That way, it will only print if you've reached the end of the loop.
@chongjin3 - look at my last code post above. all you have to do is add the logic to do what you want
Join our real-time social learning platform and learn together with your friends!