+ 1
In python how do i make this code?
n = new o = option if n is equal to or less than 2 then o is 1 else if n is equal to or less than 8 and n is greater 2 then o is 2 print (o)
5 Answers
+ 4
Use if else like this:
if condition:
alternative_a
elif other_condition:
alternative_b
else:
alternative_c
If you use while n <= 2 without changing n within the statement you'll create an infinite loop.
n = 0
while n <= 2:
print('Hello')
This is an infinite loop because n isn't changed and n <= 2 will always be true.
n = 0
while n <= 2:
n += 1
This is not an infinite loop because n is changed and the loop will only continue as long as n is <= 2
+ 7
You have created an infinite loop.
While (TRUE):
do this
This is the case with your code where condition is always true.
In simple words, avoid using while loops without any update statement.
While is generally used to check on a large array of data.
Remember, 'if' is your best friend.
Well explained Anna
0
should I use a while instead of if so the then is not needed?
0
or is there a better way?
0
a = massNumber=int(input(""))
p = protons=int(input(""))
n = neutrons = a - p
print ('number of neutrons in the atom;')
print (neutrons)
while n<=2:
o = orbitals = 1
print (o)
this is what I have but it keeps timing out.