Why does this loop only once? [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
while count > 0:
if count = 0:
return n
elif count < 0:
print(" ") # prints empty if n is below 0
else:
count = count - 1
collect += math.ceil((n - 5)/2)
return collect
The inputs are (1003, 3) - result is 499, which means it just loop once and subtracts 5 and then divides by 2, then it stops. Anyone know why?

Your inner conditionals don't make sense with the while. And you have a return statement in the loop, so yes, it only looped once.
Start with this
import math
n, count = (1003, 3)
print("N = " + str(n))
while count > 0:
n = math.ceil((n - 5) / 2) # Update this to do whatever your logic is
print(count, n)
count -= 1
if n < 0:
print("N is negative")
else:
print("N = " + str(n))

You have several problems.
First, your syntax isn't indented evenly.
Second, your if statement have = instead of ==. The first one for assigning values to variables, the second is for checking equality.
Third, you have a return statement that's going to exit from whatever function this loop is inside.

Related

My "test if this is a prime number" code crashes when I test numbers greater than 4 [closed]

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 last month.
Improve this question
I'm a Python noobie, please have mercy.
I tried to create a code to test if a given number is a prime number. I tried the %, sqrt, and n methods. All ended with the same result: when I try to test the code for numbers greater than 4, the code returns NULL and says "Process finished with exit code 0"
Can you please help me figure out what I'm doing wrong?
Here's the code I wrote (this is the 'n' method where I apply % to numbers all the way up to n; I know it's not the most efficient, but I'll fix that after I make sure it works):
x = 5
if x > 1:
for i in range(2, x):
if (x % i) == 0:
print("NOT prime")
break
else:
print("YES, it's prime")
else:
print("NOT prime, because the number is < 1")
break should be indented once more, so it does not break the loop when it is not a prime.

Write a python program to accept a number from a user and then calculate the average of all [closed]

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 3 months ago.
Improve this question
Write a python program to accept a number from a user and then calculate the average of all numbers from 1 to that given number. Then write the trace table to track the values of the variables at each iterate.
This is my attempt:
Range=int(input("ee: "))
Sum=0
Average=0
for i in range(1,Range):
sum=sum+i
Average=sum/i
print(Average," ",i)
I get an error on line 4.
sum is a keyword, you meant to use Sum (The variable you declared) in line 5 and 6.
Range=int(input("ee: "))
Sum=0
Average=0
for i in range(1,Range + 1):
Sum=Sum+i
Average=Sum/i
print(Average," ",i)
Also notice I have made it Range + 1. This is because the limit is not inclusive in the range() function.
You could also make the code a little compact like this:
count = int(input("ee: "))
total = sum(range(1, count + 1))
average = total / count
print(average)
Or just
count = int(input("ee: "))
average = sum(range(1, count + 1)) / count
print(average)

It shows the error as 'fact' not defined.why? [closed]

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
def factorial(n):
if(n == 1):
fact = 1
return fact
else:
fact = a * factorial(n - 1)
return fact
a = int(input("Enter a number to find its factorial: ")
if (a > 0):
print("The factorial of", a, "is:", fact)
else:
print("Enter a positive value to find its factorial")
In the above code it tells me that - NameError name 'fact' is not defined .
Lines starting from a = int... should be outside your function. Once that is done all you need is to add fact = factorial(a).
Find the correct logic below.
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
# check if the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))

Issue with if-else statements [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
This is what I've input:
def greater_less_equal_5(answer):
if 6 > 5:
return 1
elif 4 < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
and gave me this note
Oops, try again. It looks like your function output 1 instead of -1
when answer is 3. Make sure you filled in the if and elif statements
correctly!
and this is what came up in the upper right display:
>1
>1
>1
>None
No matter how I change around the numbers and the >/< I've even tried == and != it still outputs 1 1 1 None.
I've searched around for any possible tips and seen others stuck on the same problem as me and when I tried their solves I then get:
def greater_less_equal_5(answer):
if > 5:
return 1
elif < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
and the output is:
File "python", line 2
if > 5:
^
SyntaxError: invalid syntax
Is this test rigged to always output a failed result to make me pay for pro and ask for their help?
And the hint given for this is:
Make sure the if and elif statements end with colons :
Your code should look something like:
if EXPRESSION:
do something
elif OTHER EXPRESSION:
do something
else:
do something
Am I just missing something horribly basic?
You are indeed missing something basic - namely, that the output of your function doesn't depend on answer at all. No matter what you feed in as answer, because 6 > 5 is always True, it will always return the result of that case.
What you need is
def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
elif answer == 5:
return 0
You're missing 'answer' variable for expression, which you pass into your function
def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)

Loop being ignored by Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm a beginner to python, and when trying to see if I could make a simple program myself, I ran into this problem:
class y:
def out(self):
print("restarting")
choice = y
choice.out
while choice == y: # loop until user stops
while j >= 0: # loop until j < 0
print('lives:', j)
j = j - 1
print('out of lives!')
print('restart?')
choice = input(' Y or N ') # Ask user to restart or not
Everything works once, but Python seems to ignore the first loop (while choice == y). Have I forgotten a step, or am I doing this wrong altogether?
I don't think you need a class y here. If you just want to loop until choice isn't the character "y", then you can use ordinary strings.
choice = "y"
while choice == "y": # loop until user stops
j = 3
while j >= 0: # loop until j < 0
print('lives:', j)
j = j - 1
print('out of lives!')
print('restart?')
choice = input(' Y or N ') # Ask user to restart or not
Result:
lives: 3
lives: 2
lives: 1
lives: 0
out of lives!
restart?
Y or N y
lives: 3
lives: 2
lives: 1
lives: 0
out of lives!
restart?
Y or N n
The program loops until the user enters a value other than "y".

Categories