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)
Related
I have an exercise:
Write code that asks the user for integers, stops loop when 0 is given.
Lastly, adds all the numbers given and prints them.
So far I manage this:
a = None
b = 0
while a != 0:
a = int(input("Enter a number: "))
b = b + a
print("The total sum of the numbers are {}".format(b))
However, the code needs to check the input and give a message incase it is not an integer.
Found that out while searching online but for the life of me I cannot combine the two tasks.
while True:
inp = input("Input integer: ")
try:
num = int(inp)
except ValueError:
print('was not an integer')
continue
else:
total_sum = total_sum + num
print(total_sum)
break
I suspect you need an if somewhere but cannot work it out.
Based on your attempt, you can merge these two tasks like:
a = None
b = 0
while a != 0:
a = input("Enter a number: ")
try:
a = int(a)
except ValueError:
print('was not an integer')
continue
else:
b = b + a
print("The total sum of the numbers are {}".format(b))
If you want to use an If-Statement, you don't need the else: If the number is not 0 it will just start again until it's 0 sometime.
total_sum = 0
while True:
inp = input("Input integer: ")
try:
num = int(inp)
except ValueError:
print('was not an integer')
continue
total_sum = total_sum + num
if num == 0:
print(total_sum)
break
Since input's return is a string one can use isnumeric no see if the given value is a number or not.
If so, one can convert the string to float and check if the given float is integer using, is_integer.
a = None
b = 0
while a != 0:
a = input("Enter a number: ")
if a.isnumeric():
a = float(a)
if a.is_integer():
b += a
else:
print("Number is not an integer")
else:
print("Given value is not a number")
print("The total sum of the numbers are {}".format(b))
i try to count divisor if number have 4 divisors will print "elegant number" else "not elegant number" but i have trouble while print it i have finished to show divisors from number but my program can't show elegant number or not. i have your opinion to fixed it. here my code:
N = int(input("enter number: "))
i = 1
factor = 0 # this for count divisors but doesn't right
while (i <= N):
if(N%i==0):
print(i)
factor+=i # this for count divisors but doesn't right
i+=1
if(factor==4):
print("elegant number")
else:
print("not elegant number")
i have put comment in line which line for count divisors.
I think the main issue inside the code was adding factor += i instead of factor += 1 (to account for a new divisor).
Also, the placement of the factor += 1 condition had to be inside the if-statement.
Here is an updated version of the code:
N = int(input("enter number: "))
factors = 0
for i in range(1,N+1): # Add N+1 to loop over "i=N" as well
if N%i == 0:
print(i)
factors += 1
if factors == 4 :
print("elegant number")
else:
print("not elegant number")
Please let me know in the comments section if you need further help. Cheers,
PS. Alternative version using a while-loop:
N = int(input("enter number: "))
factors = 0
i = 1
while i<=N:
if N%i == 0:
print(i)
factors += 1
i += 1
if factors == 4 :
print("elegant number")
else:
print("not elegant number")
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))
I am trying to learn Python basics. I am designing a code that would print a triangle, code posted below. The expected output of the program must be a triangle from the right side not from the left which part of the code to modify to get the expected output.
The output of the program should not be like below
I want the output to be like this
Any help on this is highly appreciated. Thanks
while True:
n = int(input("enter a number between 0 and 10:"))
if 0 <= n <= 10:
break
print('try again')
rows = n
for num in range(rows+1):
for i in range(1, num+1):
if num % 2 == 0:
#print(end="" '#')
print('#',end="")
else:
#print(num)
print(num,end="")
print(" ")
We can use a list to create the elements (when it's an odd number just append the value in number times otherwise character '#' number times as the description said )of the triangle and then use a for loop to print them as below:
while True:
n = int(input("enter a number between 0 and 10:"))
if 0 <= n <= 10:
break
print('try again')
result = [idx*'#' if idx % 2 == 0 else f'{idx}'*idx for idx in range(1, n+1) ]
for i in range(len(result)):
print(' '*(n-i-1), result[i])
The result would be:
1
##
333
####
55555
Hope this can help you :)
Checking your code with the input (5) gives the output (1##333####55555 )
but the following code for the same input (5) gives the output (55555####333##1 )
while True:
n = int(input("enter a number between 0 and 10:"))
if 0 <= n <= 10:
break
print('try again')
rows = n
for num in range(rows, 0,-1):
for i in range(num+1, 1,-1):
if num % 2 == 0:
#print(end="" '#')
print('#',end="")
else:
#print(num)
print(num,end="")
print(" ")
Hope this answer your question
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")