python else statement confusion [duplicate] - python

This question already has answers here:
Why is the purpose of the "else" clause following a "for" or "while" loop? [duplicate]
(2 answers)
Closed 2 years ago.
I am new to python programming. I am a little confused in the below code, how else statements work without corresponding if statement. Could anyone please explain to me the below code. Program is Prime number between two intervel.
start=int(input("Enter Number: "))
stop=int(input("Enter Second Number: "))
for i in range(start,stop):
if i>1:
for j in range(2,i//2+1):
if(i%j==0):
break
else:
print(i)

In Python, loops can have an else statement. However, think more of a try-except statement than a if clause.
An example:
for item in container:
if search_something(item):
# Found it!
break
else:
# Didn't find anything..

Related

Python if statement logic confusion [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
can someone explain me why when I use this if statement:
if template_decider != ("B" or "P"):
something_went_wrong()
else:
print("Hello")
If user inputs B the statement returns "Hello" but if user press P it resturns function something_went_wrong?
I'm really confused why it not always returns "Hello"
Thank you.
You wrote incorrect statement. But it's possible to do what you want with this code:
if template_decider not in ("B", "P"):
something_went_wrong()
else:
print("Hello")

Else in For Loop example [duplicate]

This question already has answers here:
Why does python use 'else' after for and while loops?
(24 answers)
Closed 2 years ago.
In the image attached, when i=3, why does the "break" take us to the final print("Out side the loop")? Why doesn't it jump to the part:
else:
print ("Loop terminates with success")?
I am struggling to understand this.
for...else loops only run the else when the loop terminates normally and a break is not encountered. So, it skips the else since the loop was ended early.

Adding error check gets script stuck in infinate loop (python) [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
What's the canonical way to check for type in Python?
(15 answers)
Closed 2 years ago.
I'm working on a script to take user input and write it into an Excel file
currently the script works but when trying to add in logic to reject non-integer answers, one of two things happen:
it gets stuck in an infinite loop after properly rejecting the first answer where it keeps asking for a new answer and never accepts it
it properly rejects the first answers, but then accepts the second no matter what it is
this is the code being used where it gets stuck in an infinite loop (I have tried a few other variations that do the same thing but this is the most recent)
true = True
x = input('What is the first marked distance?')
while true == True:
if x == int:
cl.value = x
break
else:
x = input('Please enter a whole number')
any advice on why this is looping and not working would be greatly appreciated
Use float(x)%1 == 0 instead of x == int

statement that executing code over and over [duplicate]

This question already has answers here:
How to run a script forever? [duplicate]
(8 answers)
Closed 3 years ago.
I do not know how to do code that is executing over and over again. I would like to achieve something like this:
(< stands for input, > stands for output)
message=input()
print('Write down the text.')
>Write down the text.
<qwerty
>qwerty
>Write down the text.
<asd
>asd
You can achieve this with a
while True:
// code you want to execute repeatedly here
The while loop will continue to execute until the condition becomes false (which it never will, in this case) so if/when you want to break out of the loop you'll need to have use a break statement
Do you mean like this?
while True:
message=input("Write down the text: ")
print(message)

In Python input returned does not get detected by my if or elif statements skips straight to else statement, why? [duplicate]

This question already has answers here:
String comparison in Python: is vs. == [duplicate]
(4 answers)
Closed 6 years ago.
I am new to programming and using Python 3.5. When ever I run the following code it skips my if and elif statement and goes straight to else after entering input values that should run in the if or the elif lines of code (ie input entered is chris or sam). I don't understand why that is considering that all values are strings.
Here is my code:
name = input('What is your name?\n')
name = name.lower()
if name is 'chris':
print('Hi Chris!')
elif name is 'sam':
print('Hi Sam!')
else:
print('Who are you?')
Thanks in advance :)
You should be using == not is. Is tests whether one object is the same type as another (if value is None for e.g).

Categories

Resources