while loop factorial non use break [duplicate] - python

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 5 years ago.
I am a beginner who started Python a few days ago.
I'm writing a code to study and to get a Factorial. I want to write a code to terminate the program when a negative number is entered (without the break statement), but the code below has not progressed for several hours. I hope you can help me!
This code works, but the condition I want to satisfy is not to use break, but to exit the program if a negative number is entered
Code >>
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
while True:
num = int(input("Enter a number: "))
if num < 0:
continue
print(str(num) + "! =", factorial(num))

Maybe:
num = <any positive number>
while num >= 0:
...

Related

New to python, Do I need a while loop statement [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 12 months ago.
I'm new to python and I am trying to figure out how do I enter an error message if the user inputs a number less than zero.
Here is my code to give you a better understanding and I thank you all in advance for any advice.
# Python Program printing a square in star patterns.
length = int(input("Enter the side of the square : "))
for k in range(length):
for s in range(length):
if(k == 0 or k == length - 1 or s == 0 or s == length - 1):
print('*', end = ' ')
else:
print('*', end = ' ')
print()
Here is a simple and straight forward way to use a while loop to achieve this. Simple setting an integer object of check to 0. Then the while loop will evaluate check's value, then take user input, if the user input is greater than 0, let's set out check object to 1, so when we get back to the top of the loop, it will end.
Otherwise, if the if check fails, it will print a quick try again message and execute at the top of the loop again.
check = 0
while check == 0:
length = int(input("Enter the side of the square : "))
if length > 0:
check = 1
else:
print("Please try again.")
You can use this code after the input statement and before for loop.
if length < 0 :
Print("invalid length")
Break

Issue with the code in finding even or odd number using for loop [duplicate]

This question already has answers here:
Check if a number is odd or even in Python [duplicate]
(6 answers)
Closed 1 year ago.
number = int(input("Type your number to check even or odd :"))
for number in range (1,100):
if(number%2) == 0:
print("This is even number")
elif number > 100:
print("Enter the valid number from 1 to 100")
else:
print("This is ODD number")
i am a beginner in python language , I have written code to read the number as EVEN or ODD in for loop condition between (1,100). correct me if making any mistakes in my code .
Why are you using for loop, just check the condition like if number > 100;the number is invalid.Check this example
nos=int(input())
if(nos>100):
print("Enter the valid number from 1 to 100 ")
else:
if(nos % 2 ==0):
print("Number is Even")
else:
print("Number is Odd")
There are mistakes in your code.
1.Indentation error at the 2nd line.(Remove whitespace before for loop.)
2.The name of the input variable and the iterator name in for loop is same. So your intended logic would run on the numbers from 1 ,2, 3 ..... 99. It never runs on the user entered value. So change the name of any variable. Both cant be 'number'.
3.Although you change the name of the variable, you initialised for loop with 100 iterations so you see output 100 times.
so if you want to check the numbers between given range which are even or odd you can try this..
num = int(input(" Please Enter the Maximum Number : "))
for number in range(1, num+1):
if(number % 2 == 0):
print("{0} is Even".format(number))
print("{0} is Odd".format(number))

Does changing variable in one function doesnot effect the variable in another function [duplicate]

This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 1 year ago.
def happy(num):
while len(str(num))>1:
finding=hfind(num)
if finding==1:
print("True")
else:
print("false")
def hfind(num):
total=0
for i in str(num):
total+=int(i)*2
num=total
return(num)
happy(100)
I wrote code for Happy Number but,i don't know why it is not printing any output.
Can anybody explain clearly especially using this problem.
You need to reassign the value of num inside the loop.
Use ** 2 to square a number.
You need to check if the number reached 4 to prevent an infinite loop.
def happy(num):
while num != 1 and num != 4:
num = hfind(num)
if num == 1:
print("True")
else:
print("false")
def hfind(num):
total=0
for i in str(num):
total += int(i) ** 2
return total
happy(100)

I am trying to find a specific digit in a number using recursion in python [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 2 years ago.
def checkLuckyDigit(num):
if num == 0:
return False
elif (num%10) == 8:
return True
else:
checkLuckyDigit(num//10)
num = int(input("Enter a number = "))
if(checkLuckyDigit(num)):
print("There is a lucky digit")
else:
print("There is no lucky digit")
In the above code i need to find out whether the user entered number has 8 in it or not , when i enter 4348 it displays There is a lucky digit but when i enter 3438434 it displays There is no lucky digit and the function returns None.
I recently moved from C++ to python so i am really confused what my mistake is.
If a Python function reaches the end with no return, it implicitly returns None, which is what happens when you hit your recursive call. You just need to change the last line to return checkLuckyDigit(num//10)
You didn't handle the unwinding condition
Do it like this
def checkLuckyDigit(num):
if num > 0:
if num % 10 == 8:
return True
if checkLuckyDigit(num//10): #This checks the unwinding condition
return True
else:
return False
num = int(input("Enter a number ="))
if checkLuckyDigit(num):
print("There is a lucky digit")
else:
print("There is no lucky digit")

How do I get user input to ONLY equal 1-8? [duplicate]

This question already has answers here:
Limiting user input to a range in Python
(5 answers)
Closed 3 years ago.
I am trying to create a loop where the user is given choices 1-8 and if they do not choose 1-8, it loops them back around re-enter number 1-8. I am attempting to use a while loop with two conditions. What am I missing?
fm_select = int(input("Enter a number 1-8"))
while fm_select <= 8 and fm_select >= 1:
Your ranges are wrong. You want the while loop to fail when they are correct, since you're trying to break out of the loop. So, you want your loop to check against every number that isn't between one and eight. Instead, do
fm_select = 0
while (fm_select < 1 or fm_select > 8):
fm_select = int(input("Enter a number between one and eight: "))
"As long as their input is less than one or higher than eight, keep asking"
something like this should work
while(True):
fm_select = int(input("Enter a number 1-8"))
if 0 < fm_select < 8:
break
print("try again")
print("you have entered %d" %(fm_select) )

Categories