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 2 years ago.
Improve this question
I'm learning Python and there is a problem where I got stuck. I would definitely appreciate it if anyone can help to solve my doubts.
When I typed this code:
i=1 and i<=10
print(i)
the output is True. Obviously i is a boolean now but I don't understand why.
For there is an "and", and 1 is less than 10, so the statement "i=1 and i<=10" is true. But why the variable i (rather than the whole statement) becomes a boolean? I thought i should still be an integer whose value is 1?
It's a beginner's question but it really confuses me. Thanks for anyone who contributes an idea!
By your code , I think that you are setting ‘1 and i<=10’ into your i variable.
If you want check equal to 1 , use ‘==‘ and add an if sentence, so your code will be:
if i==1 and i<=10:
print(i)
The statement is to the right of the assignment operator =
1 and i<=10
i is not defined at this point in your example, so a NameError would be raised. I assume that you defined i somewhere before running this code and didn't see the error.
Had i been defined, the result of calculating this statement would be assigned back to i as it is to the left of the assignment operator =. Its the same as
i = (1 and i <= 10)
If you want to assign i before the comparison, then it needs to be in another statement
i = 1
i <= 10
Related
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 1 year ago.
Improve this question
I'm very new to coding and I am learning python alone and this is my code :
from random import *
temp =Randint( 0,70 )
print(temp)
if : temp = 69
print("nice")
You made a few errors
randint is a function so it's first letter should be small case
The for loop syntax was wrong
And we use == for checking equality and = for assignment
You can try the following code below
from random import randint
temp = randint( 0,70 )
print(temp)
if temp == 69:
print("nice")
Your if statement isn't quite right. The colon needs to go at the end of the line, and in an if statement you need a double equals sign (==, which compares two elements) rather than a single equals sign (which assigns a variable). Additionally, you need to indent (put a tab before) lines that are part of a block. You can read more about Python if statements here. The fixed if statement should look like this:
if temp == 69:
print("nice")
Lastly, since Python names are case-sensitive, randint must be in all lowercase.
I hope this is helpful!
A quick thing to remember is that “==“ means two things are equal and “!=“ means if something is not equal.
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 5 years ago.
Improve this question
I am very new to Python and trying to learn by trial-and-error, so my question may sound naive for the community.
Let's say I have two empty lists with only the first element defined:
a = [[]]*20
a[0] = 0
b = [[]]*20
b[0] = 1
I want to use a for loop for creating the other elements of the lists:
x = 20
for i in range(1,x):
a[i] = b[i-1],
b[i] = a[i-1]+b[i-1]
What I obtain is the following error:TypeError: can only concatenate tuple (not "int") to tuple.
Basically I am trying to reproduce the fibonacci series (a famous starting point in Python tutorial), but I would like to experiment other ways of obtaining the same output.
Thank you!
The problem is on this line:
a[i] = b[i-1],
Notice the comma at the end? That makes python think you're dealing in tuples. Remove it and the error will be gone.
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 6 years ago.
Improve this question
I'm still getting a handle on python and have been trying to implement a REPEAT UNTIL loop using online tutorials. Everything seems to be in order but I keep getting a syntax error but I absolutely cannot find an error with my syntax! Can someone help me? My code is;
while detvar != "SABRE":
REPEAT
detvar=input("Please Pass a Valid Detector or Parameter Set");
UNTIL detvar = "SABRE"
detvar is my string variable the error is for the 'detvar' on the last line.
This is all you need:
detvar = "" # allow for at least one iteration
while detvar != "SABRE":
detvar=input("Please Pass a Valid Detector or Parameter Set")
REPEAT and UNTIL are not valid expressions in Python. Instead, you want to use while condition != value, which is what you originally had.
The while statement allows you to continue iterating as long as a condition holds true. Alternatively, you can repeat until something is true by negating the condition.
So, while detvar != "SABRE": iterates the body of the loop (which is everything indented under the colon) until detvar is equal to "SABRE".
Edit: In accordance with Bryan Oakley's comment, detvar is initialized as a value that is not "SABRE" so that the loop body executes at least once.
This is a horribly worded question, and I have no idea what this code is meant to accomplish, but I'll see if I can decipher this. There's no need to use "REPEAT," just do
while devtar != "SABRE":
devtar = input("Please Pass.(whatever this is).. Set")
It should exit the loop when devtar = "SABRE" automatically.
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 7 years ago.
Improve this question
A problem exists in Python 2.7.11, with the print function:
elif e=="randomize w and x":
random=randint(int(w),int(x))
print random
elif e=="randomize w and y":
random=randint(int(w,int(y))
print random
The boldfaced print shows up as a syntax error, yet all 278 others in my program do not. Why this is, and how I fix it?
The problem is that in
random=randint(int(w,int(y))
a close parenthesis after w is missing, therefore Python thinks the expression continues on next line, but print at that point is a syntax error.
Your problem is not with the print statement, rather the line right before it. The line before hass inbalanced parenthesis:
random=randint(int(w,int(y))
Make sure you balance them out (add an extra ) at the end), and your error on the next line will disappear.
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 8 years ago.
Improve this question
I am new to python and I have this script to set a variable:
set python = 1
print (python)
I have tried this
set (python) = 1
print (python)
But this doesn't seem to work ether. Can any one help.
This is not Python. In Python, to set a variable, use:
name = 1
And reassign:
name = [1, 2, 3]
EDIT:
The exercise asks you to set the variable my_variable to 10.
So use:
my_variable = 10 # set my_variable to 10, and use your brain
No need to write set. Its not a python syntax.
Python is dynamically typed and creates objects as it goes along. You don't really have constants or anything along those lines (at least at a beginners level - I'm not sure about advanced levels but I doubt it).
As a result whenever you declare a variable just input:
RandomVariableName = 1
or
RandomVariableName = "one"
Python will automatically figure out what the type of object is and assign it to that name (loosely speaking).