I'm trying to figure out how to print the final line of code for this - whether it's a prime number or not. I can't seem to get it to print with the code I have. Any help would be appreciated. Thanks!!
number = int(input("Enter a positive number to test: "))
while number <= 0:
print ("Sorry, only positive numbers. Try again.")
number = int(input("Enter a positive number to test: "))
test = 2
number1 = number - 1
for x in range (0, number1):
trial = number % test
if trial != 0:
print (test, "is NOT a divisor of", number, "...")
break
print (number, "is a prime number!")
else:
print (test, "is a divisor of", number, "...")
break
print (number, "is not a prime number!")
test = test + 1
The break statement ends the execution of the branch. The following print statement is never reached.
To get the correct functionaility use a boolean value and perform the check at the end:
is_prime = True
for x in range (2, number):
trial = number % x
if trial != 0:
print (x, "is NOT a divisor of", number, "...")
else:
is_prime = False
print (x, "is a divisor of", number, "...")
if is_prime:
print (number, "is a prime number!")
else:
print (number, "is not a prime number!")
You also do not need to use a variable test. Use the x from your range directly.
Have a look at the Python reference for the keyword for more information.
Syntax:
if expression:
statement(s)
else:
statement(s)
It executes all statements with break so print will be never happen...
Related
I can't figure out how to execute a condition that check if the user inputs are equal before checking if it is a prime number. My goal is to ask a user to input two prime numbers. And in the 2nd while loop, I want to make sure that the 2nd number is not equal to the first number in order to be checked if it is a prime number
value_P=[]
value_Q=[]
def is_prime(num):
if num == 2:
return True
if num < 2 or num % 2 == 0:
return False
for n in range(3, int(num**0.5)+2, 2):
if num % n == 0:
return False
return True
while True:
try:
P = int(input("Enter a prime number(P): "))
if is_prime(P):
value_P.append(P)
print("P =", value_P)
break;
else:
print(value_P, "is not a prime number")
except ValueError:
print("Provide an integer value...")
continue
#2nd while loop
while True:
try:
Q = int(input("Enter a prime number(Q). Not the same as the number you entered above: "))
if is_prime(Q):
value_Q.append(Q)
print("Q =", Q)
break;
else:
print(value_Q, "is not a prime number")
except ValueError:
print("Provide an integer value...")
continue
You can check if the value of Q is inside the P list using an if statement in the 2nd while loop.
while True:
try:
Q = int(input("Enter a prime number(Q). Not the same as the number you entered above: "))
if is_prime(Q):
#here the code made sure it's prime, next statement checks if the "Q" input was already previously used as the "P" input. If it is, the loop breaks.
if Q in value_P:
break
else:
value_Q.append(Q)
print("Q =", Q)
break;
else:
print(value_Q, "is not a prime number")
except ValueError:
print("Provide an integer value...")
continue
I want to check the Even or Odd numbers therefore I have written the following code:
number = int(input("Which number do you want to check? "))
if number % 2 == 0:
print("This is an even number")
else:
print("This is an odd number")
It gives the following result:
Which number do you want to check? 20
This is an even number
But when is change the above code inversely like this
number = int(input("Which number do you want to check? "))
if number % 2 == 0:
print("This is an odd number")
else:
print("This is an even number")
It gives the following result:
Which number do you want to check? 20
This is an odd number
even if I change the "If modulo operation" to % 7 == 2 it would still give even as odd or vice versa
if number % 2 == 0: print("This is an even number")
else: print("This is an odd number")
But when is change the above code inversely like this
if number % 2 == 0: print("This is an odd number")
else: print("This is an even number")
You only switched the print statements but you didn't update the if condition.
We know that if an integer mod 2 is 0, then it is even... or if an integer mod 2 is 1, then it is odd. So you should change the condition of the bottom piece of code to if number % 2 != 0
try:
num=int(input("Enter a number:"))
def sum(num):
result=0
if num < 0:
print(num, "is not a natural number!")
else:
for i in range(1,num+1):
result=result + (i*i)
return result
print("The sum of square of first", num, "natural number is:", sum(num))
except ValueError:
print("Invalid Input")
For the given code;
How can I not execute the below statement for input less than zero?
print("The sum of square of first", num, "natural number is:", sum(num))
Putting this statement inside else block did not help!
Just add return before the else. It should work. You can simply write return, or you can actually return a value depending on your necessities
In python, you can write if-else statements in one line
Eg:
take_this if this_condition_is_True else take_this
So now if the num is negative the sum() function won't even be called
try:
num=int(input("Enter a number:"))
def sum(num):
result=0
if num < 0:
print(num, "is not a natural number!")
else:
for i in range(1,num+1):
result=result + (i*i)
return result
print(f"The sum of square of first {num} natural number is: {sum(num)}" if num>=0 else "Stop giving negative numbers")
except ValueError:
print("Invalid Input")
I'm very much new to Python and have been trying out new exercises online and I've completed a few with If/Else/Else If.
This new program I'm trying to run asks the user to enter an integer and depending on what they enter the print feedback will be different. The program must us a Nested IF.
The first statement for 'entered is zero' is working perfectly. But when I enter an even number it still says 'Odd' and I can't work out why and I've been looking at many different tutorials.
Perhaps I'm not using the Nested If Statement in the correct manner?
All help very appreciated.
Thanks!
enter = int(input("Enter an Integer: "))
option = enter % 2
if (enter == 0):
print("The number you entered is zero")
if (option % 2) > 0:
print("The number you entered is larger than zero and even")
else:
print("The number you entered is larger than zero and odd")
Since, you specifically asked for nested if statements, check the answer below:
enter = int(input("Enter an Integer: "))
if enter > 0:
if enter % 2 == 0:
print ("Number entered is greater than 0 and even")
else:
print ("Number enetred is greater than 0 and odd")
else:
print ("Number entered is less than or equal to 0")
x = int(input("Enter an Integer: "))
if (x >= 0):
if (x ==0) :
print("Number is 0" %x)
elif (x %2 == 0):
print (" Number %d is a positive even number" %x)
else:
print ("Number %d is a positive odd number" %x)
else:
if (abs(x)%2 == 0):
print ( "Number %d is a negative even number" %x)
else:
print ( "Number %d is a negative odd number" %x)
I have a Python assignment that is as following: "Write a complete python program that asks a user to input two integers. The program then outputs Both Even if both of the integers are even. Otherwise the program outputs Not Both Even."
I planned on using an if and else statement, but since I'm working with two numbers that have to be even instead of one, how would I do that?
Here is how I would do it if it was one number. Now how do I add the second_int that a user inputs???
if first_int % 2 == 0:
print ("Both even")
else: print("Not Both Even")
You can still use an if else and check for multiple conditions with the if block
if first_int % 2 == 0 and second_int % 2 == 0:
print ("Both even")
else:
print("Not Both Even")
An even number is an integer which is "evenly divisible" by two. This means that if the integer is divided by 2, it yields no remainder. Zero is an even number because zero divided by two equals zero. Even numbers can be either positive or negative.
Use raw_input to get values from User.
Use type casting to convert user enter value from string to integer.
Use try excpet to handle valueError.
Use % to get remainder by dividing 2
Use if loop to check remainder is 0 i.e. number is even and use and operator to check remainder of tow numbers.
code:
while 1:
try:
no1 = int(raw_input("Enter first number:"))
break
except ValueError:
print "Invalid input, enter only digit. try again"
while 1:
try:
no2 = int(raw_input("Enter second number:"))
break
except ValueError:
print "Invalid input, enter only digit. try again"
print "Firts number is:", no1
print "Second number is:", no2
tmp1 = no1%2
tmp2 = no2%2
if tmp1==0 and tmp2==0:
print "Both number %d, %d are even."%(no1, no2)
elif tmp1==0:
print "Number %d is even."%(no1)
elif tmp2==0:
print "Number %d is even."%(no2)
else:
print "Both number %d, %d are NOT even."%(no1, no2)
Output:
vivek#vivek:~/Desktop/stackoverflow$ python 7.py
Enter first number:w
Invalid input, enter only digit. try again
Enter first number:4
Enter second number:9
Firts number is: 4
Second number is: 9
Number 4 is even.