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)
Related
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
How do I modulo an input by two to check if the number is even or odd?
here is my code
num = input("Enter a number: ")
mod = num % 2
if mod > 0:
print("You picked an odd number.")
else:
print("You picked an even number.")
You need to take the int value of the input. int(input("Enter a number: "))
You need to convert the input to an integer like below:
num = int(input("Enter a number: "))
mod = num % 2
if mod > 0:
print("You picked an odd number.")
else:
print("You picked an even number.")
% is for string formatting for strings, for integers it's for modulo.
There is no meaning to test num%2>0. num % 2 is the remainder of the integer division of num by 2. num is even if and only if this remainder equals 0. So the predicate is_even(n) corresponds to num % 2==0.
Your code becomes :
num = int(input("Enter a number: "))
if num % 2==0:
print("You picked an even number.")
else:
print("You picked an odd number.")
You should consider defining a function and then call it like this:
def is_even(n):
return n%2==0
n = int(input("Enter a number:"))
if is_even(n): print("You picked an even number")
else: print("You picked an odd number")
This is my code:
from sys import exit
import random
number = random.randint(1, 10)
count = 0
def guess():
print ("Input a number 1 - 10")
guess = input()
if guess == "I give up":
print ("The correct number was", number,"!")
print ("You tried", count, "times before giving up!")
exit(0)
else:
if guess == (number):
print ("CORRECT!")
print (count, "failed attempts.")
exit(0)
else:
print ("WRONG!")
print ("Try again!")
global count
count += 1
while True:
guess()
If I run this, I can keep guessing but never get the correct number. I said I gave up, and it gave me the correct number. But I already guessed that number, so I don't know what's the problem.
You are taking the input as a string rather than an integer. Thus, you may be comparing "5" to 5, which is false. Instead, call if int(guess) == (number)
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...
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.