I tried to check if the number is negative or not:
x = int(input('Enter any number:'))
bumble = x+1
shumble = x-1
if shumble>x:
print('Your number is a negative number')
elif bumble>x:
print('Your number is a positive number')
elif x == 0:
print('Your number is 0')
but the problem is python won't consider a negative number its mathematical value
and I checked that by running this block of code
x = -2
y = 1
if x>y:
print('-2 is greater that 1')
elif y>x::
print('1 is greater than -2')
and the output says:
-2 is greater than 1
so can someone pls help me find a solution?
I would really appreciate it!
I'm not sure whether or not you're not allowed to use zero with you inequality operator. But surely the following should work pretty well. You can omit the exception handling if not needed.
try:
x = int(input('Enter any number:'))
if x < 0: print('Your number is a negative number')
elif x > 0: print('Your number is a positive number')
elif x == 0: print('Your number is 0')
except ValueError:
print("That was not a valid number...")
You're overcomplicating this so much. To check if its a negative number check if its less than, equal to, or greater than zero
try:
x = int(input("Enter a number"))
except:
print("invalid number")
if x > 0:
print("Positive")
elif x < 0:
print("Negative")
else:
print("Equal to 0")
num = float(input("Enter a number: "))
if num > 0:
print("{0} is a positive number".format(num))
elif num == 0:
print("{0} is zero".format(num))
else:
print("{0} is negative number".format(num))
Related
I wrote the following code but every time I submit it to Hackerank.com a mistake is popping out. What should I do to solve this issue?
ValueError: invalid literal for int() with base 10: 'n= int(input("Enter any number: "))'
if __name__ == '__main__':
n = int(input("Enter any number: ").strip())
if n % 2 != 0:
print("Weird")
else:
if n >= 2 and n <= 5:
print("Not Weird")
elif n >= 6 and n <= 20:`enter code here`
print("Weird")
elif n > 2`enter code here`0:
print("Not Weird")
In the above code you are converting the input to integer. Hackerrank will have a n number of test cases. One test case among those cases might have a character has input. When you try to convert a string character to int python will throw an error saying invalid int literal. Below code can be helpful.
if __name__ == '__main__':
n = input("Enter any number: ").strip()
if n.isdigit():
if n % 2 != 0:
print("Weird")
else:
if n >= 2 and n <= 5:
print("Not Weird")
elif n >= 6 and n <= 20:
print("Weird")
elif n > 20:
print("Not Weird")
else:
print("Not all characters are integers")
I'm trying to solve the scenario with these conditions:
Ask the user to enter a number
Count up from 1 to the number that the user has entered, displaying each number on its own line if it is an odd number. If it is an even number, do not display the number.
If the user enters a number that is 0 or less, display error
My codes are as follows and I can't seem to satisfy the <= 0 print("error) condition:
num=int(input("Enter number: "))
for x in range(num):
if x % 2 == 0:
continue
print(x)
elif x<=0:
print("error")
Your solution will be :
num=int(input("Enter number: "))
if num <= 0:
print("Error")
else:
for i in range(1, num + 1):
if i % 2 == 0:
continue
print(i)
You need to print the error before looping from 1 to num because if the value is less the 0 then the loop won't run. I hope you understand.
You have to check the condition num <= 0 as soon as the user enters the number:
num = int(input("Enter number: "))
if num <= 0:
print("error")
else:
for x in range(num):
if x % 2 == 0:
continue
print(x)
Very new to python and trying to figure out how to use the and operator to check if a number is between 50 and 100. Tried using and, && and || , but just getting invalid syntax python parser-16 error. If I take the and or alternative out of the code, then it partly works and dosn't give me a error message, though it dosn't check if the value is below 100, so presumly it must the and part that I'm doing wrong?
x = int(input("Enter a number between 0 and 100"))
if x < 50:
print("That is below 50!")
elif x > 50 and < 100:
print("That is above 50!")
else:
print("That number is too high!")
close!
x = int(input("Enter a number between 0 and 100"))
if x < 50:
print("That is below 50!")
elif x > 50 and x < 100:
print("That is above 50!")
else:
print("That number is too high!")
if x > 50 and x < 100 you have to reference it each time you check if its true
Alternative solution:
x = int(input("Enter a number between 0 and 100: "))# for a better look
if x < 50:
print("That is below 50!")
elif 100 >= x >= 50:# the numbers 50 and 100 shall be inclusive in one of the three params
print("That is between 50 and 100!")
else:
print("That number is too high!")
To simplify it more, you can write as below.
x = int(input("Enter a number between 0 and 100"))
if x < 50:
print("That is below 50!")
elif 50 < x < 100:
print("That is above 50!")
else:
print("That number is too high!")
If x<50:
print('Below 50')
elif x>50 and x<100:
print('Between 50 and 100');
else:
print('Above 100');
Try this
I have written a code to find out if a number is prime or composite.
The code works fine when I input a prime number but when I input a composite number the output is:
enter number: 100
The number is not prime.
The number is prime.
I don't want The number is prime output for composite number input.
Here is my code:
print ('This program tells whether the number is prime or not')
print ('')
def prime(x):
if x < 2:
print('The number is not prime.')
else:
for n in range(2, x - 1):
if x % n == 0:
print('The number is not prime.')
break
print('The number is prime.')
i = input('enter number: ')
prime(int(i))
Please tell me what can I do to correct it.
The problem is the indentation, you've to move the indentation of the last line and add a break after that, so try using:
print ('This program tells whether the number is prime or not')
print ('')
def prime(x):
if x < 2:
print('The number is not prime.')
else:
for n in range(2, x - 1):
if x % n == 0:
print('The number is not prime.')
break
print('The number is prime.')
break
i = input('enter number: ')
prime(int(i))
I can see why. you are missing else after if. try this:
print ('This program tells whether the number is prime or not')
print ('')
def prime(x):
if x < 2:
print('The number is not prime.')
else:
for n in range(2, x - 1):
if x % n == 0:
print('The number is not prime.')
break
else:
print('The number is prime.')
i = input('enter number: ')
prime(int(i))
if num > 1:
for n in range(2, x-1):
if x % n == 0:
print('The number is not prime.')
break
else:
print('The number is prime.')
else:
print('The number is not prime.')
Simply fix that indentation in the for loop. Also, this looks a lot cleaner.
This is the recommended way to solve this problem.
do not use hard coded print statement.
try to return True or False instead.
def is_prime(x:str):
if x < 2:
return False
else:
for n in range(2, int(x/2)): # Use this for more speed
if x % n == 0:
return False
return True
Now you can check the number is prime or not by calling this is_prime function
print('Number is prime' if is_prime(6) else 'Number is not prime')
The problem is that when you break the loop the last print statement is called. If you end the function using return statement you will not reach the last print statement.
def prime(x):
if x < 2:
print('The number is not prime.')
else:
for n in range(2, x - 1):
if x % n == 0:
print('The number is not prime.')
return
print('The number is prime.')
number = int(input("Please enter a positive number? "))
if number >= 0:
for i in range(1, number + 1):
perfect_squares = i**2
print(perfect_squares, end=" ")
elif number < 0:
print("Error: you entered a negative number")
If I input the number 10 for example, I want it to output "1 4 9" right now it is outputting all of the perfect squares from 1 to 10.
How to a end the loop to only do the numbers up to the inputted number?
Just change the range to stop at square root of the number user entered:
number = int(input("Please enter a positive number? "))
if number >= 0:
for i in range(1, int(number ** 0.5 + 1)):
perfect_squares = i**2
print(perfect_squares, end=" ")
elif number < 0:
print("Error: you entered a negative number")
Output:
Please enter a positive number? 10
1 4 9
try this
number = int(input("Please enter a positive number? "))
if number >= 0:
for i in range(1, number + 1):
perfect_squares = i**2
print(perfect_squares, end=" ")
if perfect_squares > i: # add if statement here
break
elif number < 0:
print("Error: you entered a negative number")