I'm learning python right now on VS and I'm learning if statements with booleans: here's my code so far:
is_toy = False
if is_toy:
print("YOU ARE A TOY")
else:
print("you're buzz lightyear!")
no matter what the terminal prints out
y
YOU ARE A TOY
when True or
y
you're buzz lightyear!
when False,
I'm not sure whether y means yes or if there's some bug or typo
As you've suggested, there's likely a typo somewhere in your code. It's coming from somewhere earlier in your code because there is no way the code snippet you shared would print y :)
Related
I would like some help with a question on Python. Does anyone know why the coding language is so complicated to learn for some people? I need an answer in 1 week from now.
Thanks.
In my opinion it isnt really that hard. Although, I do forget things here and there. I’d suggest using 3.6 if you are a beginner. Its where I started. If you need a sample program(s), you may use the following.
Printing to console:
print(‘hello’)
Printing to console using variables:
thisIsAString = ‘hello’
print(thisIsAString)
Here are some conditions.
TrueBoolean = True
TrueString = ‘Yes’
TrueInt = 1
TrueList = [‘Yes’, 1, True]
if TrueBoolean:
print(‘Boolean is True’)
if TrueString == ‘Yes’:
print(‘String is “Yes”’)
if TrueInt == 1:
print(‘Int is “Yes”’)
if TrueList == [‘Yes’, 1, True]: # TrueList.contains(INSERT ITEMS HERE) works too.
print(‘List is true’)
else=(x = raw_input("Enter\n")):
print(x)
I have tried doing this basing it off of the correct statement...
x = raw_input("Enter\n")
print(x)
if your doing an if statement and you wanted the input after else is ran you would just run it as normal in the if/else flow.
But depending on what you are actually trying to do its likely to have a better way to do it. Not sure your skill level but i would HIGHLY recommend Jessica McKellers basic tutorials on youtube or the basic Python tutorial like Tigerhawk recommended.
if something:
print("Hello")
else:
x = raw_input("Enter Something ")
print(x)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I just started attempting to learn code via youtube and MIT lectures they have online.
I am using compileonline.com on the computer I'm coding on while the teacher is using the actual Python program. (Same version) I have all the code the same as he does
def sqrt(x):
ans = 0
if x >= 0:
while ans*ans < x: ans = ans + 1
if ans*ans != x:
print x, 'is not a perfect square.'
return None
else: return ans
else:
print x, 'is a negative number.'
return None
Now when I add print statements to the code to test it
print sqrt(16) -- then compile and I get "4" Nothing else.
then
print sqrt(34) -- compile and I get "34 is not a perfect square" followed by "None" on the next line. In the video the guy does his testing I guess on a different screen than where his code is written and he can simply type "sqrt(16)" instead of "print sqrt(16)" when he does, he gets 4 like I do. But when he types "sqrt(34)" he gets "34 is not a perfect number" and thats it, no "None" below it. This also happens for me when I test with a negative number.
My question I guess is why am I getting "None" when I use a negative number or a number that is not a perfect square?
My apologies that this is probably a terribly stupid question but I am simply trying to get exactly what he gets to ensure I'm doing this correctly and not missing something that could potentially be important. I also wonder if this is because I am using a website and he is using the actual program?
The screen/window/box (whatever its called, its not where the code is written) the professor is using looks like this
>>>sqrt(16)
4 (his answer)
>>>sqrt(34)
34 is not a perfect number. (his answer)
(This is where I get "None")
Again, my apologies for this being overly dramatic and incredibly simple. Just not trying to miss something and do everything/get the exact answers the professor is getting.
You're running into confusion in the interactive terminal because of the distinction between print statements and return values.
When you run the function with a perfect square it returns an integer which is displayed in the console. When you run the function using a non-perfect square it prints a value, which is written to the console (sys.stdout), then it also returns None, which is also displayed in the console.
When you type an expression into the Python interactive prompt, it normally prints the result of that expression. However, when the expression returns None, it doesn't print that return value. In either case, if the function you're calling performs an explicit print, that data is printed. That's what was happening in the video: when he called it with a square, it returned the square root, and the interactive loop printed that return value; when he called it with a non-square, the function printed 34 is not a perfect square, and returned None, which the interactive loop didn't print.
The difference in your case is that you wrote print sqrt(34). So you're telling it explicitly to print the return value. This has no special exception for the None value, so you printed None in the cases where the function returns that.
You are "getting" None because you return None.
If you want to eat the None try something like:
a = sqrt(34)
if a: print (a)
I just came accross the following code in an existent project, which I'm working on:
if True:
x = 5
y = 6
return x+y
else:
return 'Something
Inside the if True are lots of conditions and some will also return the function already.
Why would somebody write in that way? The code contained some other bugs also, but was just wondering about the if True: statement as it didn't make any sense to me. Probably also pretty stupid to ask it, but was wondering hehe.
It might be a remnant of debugging or refactoring. It may be that instead of True, there was orginally a condition or variable there but it has now been replaced by True. The developer perhaps left it there without refactoring or cleaning it up all the way.
If you're free to edit the code as you wish and you're sure that the else is no longer needed, then you can remove it. It indeed makes no sense to have code in your codebase that will never run.
True doesn't necessarily mean True
True = False
if not True :
print "True is false" # This prints ok
Honestly, I don't think anyone would code like this.
Does not make any sense to me, my guess is that someone wanted to have two distinct code paths that he could alternate between a'la using #if 1 .. #else -> #if 0 ... for debugging or such purposes.
Other possibility was that, as #SimeonVisser suggested, the original developer was refactoring or cleaning up the code (and did not have an emulator that allows one to easily remove 1 step of indentation from a block of code)
It could be a flag used for debugging.
It's simply used to ensure that the else: block is never executed.
I have used if True: for some blocks to ensure that my code really does what I want. Usage for debugging or refactoring.
All in all it makes no real sense to use this in an application but for testing or debugging it's somehow acceptable.
Hey I am having problems with calculating the roots of the quadratic equation with the quadratic formula, using python's complex number functionality.
When I try
>>> if root<0:
root=abs(complex(root))
j=complex(0,1)
x1=(-b+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
else:
I get the error message
SyntaxError: invalid syntax
Then, when I try instead
>>> if root<0:
root=abs(complex(root))
j=complex(0,1)
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
break
I get the error
SyntaxError: 'break' outside loop
I am trying to put:
else:
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
under it but it won't work.
Any help please?
Not sure I understand your problem, but it looks like you are not properly indenting -- Python uses white space to mark blocks, so the above should look like:
if root<0:
root=abs(complex(root))
j=complex(0,1)
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
else:
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
although that doesn't make sense because x1 and x2 are calculated the same in both branches, and j is not defined in the else branch... so maybe what you really want is
if root<0:
root=abs(complex(root))
j=complex(0,1)
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
Part of my confusion is the prompt: enter code here -- this is not a standard Python prompt so either you changed your prompt or you are using some other program with your Python. At any rate, hope this helps.
try importing the complex math module, available online in a number of forms. I believe there is an implementation for complex numbers in the standard python distributions, as well as in numpy/scipy. You can also try working out the real and complex components of your roots separately (by including a test based on the value of the discriminant). Also, your if and elif tests are identical.
As #bythenumbers points out, your if and elif conditions are the same. Also, are you getting exceptions or the wrong values? Also also, where you have j+sqrt(root), do you perhaps mean j*sqrt(root)?