I want to check the value range of an input number, then print the correct "size" (small, medium or large). If the value is out of my acceptable range, then I want the else statement to print out that the number is not valid.
Minimal example for my problem:
n = int(input("number= "))
if 0 <= n < 5:
a = "small"
if 5 <= n < 10:
a = "medium"
if 10 <= n <= 20:
a = "large"
print("this number is",a)
else:
print("thats not a number from 0 to 20")
According to Google, this is a problem with indentation. I've tried multiple ways of indenting this; I can fix the syntax, but I can't get the logic correct.
Let's fix your immediate issue: you have an else with no corresponding if statement. Syntactically, this is because you have an intervening "out-dented" statement, the print, which terminates your series of ifs.
Logically, this is because you have two levels of decision: "Is this a number 0-20?", and "Within that range, how big is it?" The problem stems from writing only one level of ifs to make this decision. To keep close to your intended logic flow, write a general if on the outside, and encapsulate your small/medium/large decision and print within that branch; in the other branch, insert your "none of the above" statement:
n = int(input("number= "))
if 0 <= n <= 20:
if n < 5:
a = "small"
elif n < 10:
a = "medium"
else:
a = "large"
print("this number is", a)
else:
print("that's not a number from 0 to 20")
You should try something like
n = int(input("number= "))
if 0 <= n < 5:
a = "small"
elif 5 <= n < 10:
a = "medium"
elif 10 <= n <= 20:
a = "large"
else:
a = "not a number from 0 to 20"
print("this number is",a)
The print statement before the else statement needs to either be removed or indented to match:
a= "large"
You've syntax (indentation) error:
n = int(input("number= "))
if 0 <= n < 5:
a = "small"
if 5 <= n < 10:
a = "medium"
if 10 <= n <= 20:
a = "large"
#print("this number is",a) indentation error in this line
else:
print("thats not a number from 0 to 20")
You can use also use following code
n = int(input("number= "))
if 10 <= n <= 20:
a = "large"
print("this number is",a)
elif 5 <= n < 10:
a = "medium"
print("this number is",a)
elif 0 <= n < 5:
a = "small"
print("this number is",a)
else:
print("thats not a number from 0 to 20")
The problem is with the print statement.
It is indented on the same level as the if block and thus, the if block ends on line containing the print statement.
Thus, the else on the next line is incorrect.
To achieve what you are trying, you should do something like this:
n = int(input("number= "))
if 0 <= n < 5:
a = "small"
elif 5 <= n < 10:
a = "medium"
elif 10 <= n <= 20:
a = "large"
else:
print("not between 0 and 20")
print("The number is", a)
The print statement needs to be placed after the else block. Also, its best to use elif statements than if statements in a situation like this.
n = int(input("Enter a number between 0 and 20: "))
if 0 <= n <= 5:
a = "small."
elif n <= 10:
a = "medium."
elif n <= 20:
a = "large."
else:
a = "invalid / out of range."
print("This number is ", a)
Related
So we have to complete a task where we enter a digit from 20-98 (inclusive). The task is to make it so that it stops counting down (or breaks) whenever there's a same-digit number (88,77,66,55,44,33,22). I am able to make it pass tests if I enter 93, but not for other numbers (for ex. 30).
If we enter a digit (for example 93), the output would be:
93
92
91
90
89
88
is my code so far:
x = int(input())
while 20 <= x <= 98:
print(x, end='\n')
x -= 1
if x < 88:
break
elif x < 77:
break
elif x < 66:
break
elif x < 55:
break
elif x < 44:
break
elif x < 33:
break
elif x < 22:
break
elif x < 11:
break
else:
print("Input must be 20-98")
I was wondering how I need to change my code so that it can apply to all numbers, and so I do0n't have to write so many if-else statements for all the possible same-digit numbers.
You can use the modulo operator (%) which yields the remainder from a division.
x = int(input())
while 20 <= x <= 98:
print(x, end='\n')
if x % 11 == 0:
break
x -= 1
else:
print("Input must be 20-98")
user_int = int(input())
if 20 <= user_int <=98:
print(user_int)
while not user_int % 11 == 0:
user_int -= 1
print(user_int)
else:
print("Input must be 20-98")
x = int(input())
if (x >= 20 and x <= 98):
while (not (x % 10 == x // 10)):
print(x)
x -= 1
print(x)
else:
print('Input must be 20-98')
i am doing a challenge but for some reason every time i run it says 3 outta of 7 test cases are incorrect and don't know why? everything seems in order. Here is the challenge if Task
Given an integer, , perform the following conditional actions:
If is odd, print Weird
If is even and in the inclusive 2 range of 5 to , print Not Weird
If is even and in the inclusive range of 6 to 20, print Weird
If is even and greater than 20, print Not Weird
My code below:
n = int(input().strip())
if n % 2 != 0:
print("Weird")
else:
if n % 2 == 1 and n in range(2,5):
print("Not Weird")
elif n % 2 == 1 and n in range(6,20):
print("Weird")
elif n > 20:
print("Not Weird")
Try this
n = int(input().strip())
if n % 2 != 0:
print("Weird")
else:
if n in range(2,6):
print("Not Weird")
elif n in range(6,21):
print("Weird")
elif n > 20:
print("Not Weird"
To include 5 and 20 in range you need to specify it as number + 1. Range does not include the last number. Also, there is no need to check for even condition every time in the else part as the control jumps to else when if fails!.
n = int(input().strip())
if n % 2 != 0:
print("Weird")
else:
if n in range(2,6):
print("Not Weird")
elif n in range(6,21):
print("Weird")
elif n > 20:
print("Not Weird")
a = float(input("Insert a floating point number:"))
n = int(input("Insert an integer number >= 0 :"))
accum = 1
count = 1
while n >= count and n >= 0:
accum = accum * a
count += 1
elif n < 0:
print("Integer value is less than 0")
if n >=0 and n < count:
print(accum)
I need to create a code which asks the user for a floating point number 'a' then an integer to use as the power of 'a' which is 'n'. I need to use a while loop and it must be valid for n>=0 only. If I don't have the elif statement then the code runs normally.
While technically you can put n >= 0 in the condition of the while loop to skip the loop if n < 0, it doesn't make sense to do so, because you never modify the value of n in the loop. It would be clearer to put the entire loop in the body of another if statement (which, incidentally, is the one which your elif—or else, as we'll see—would naturally belong to.)
a = float(input("Insert a floating point number:"))
n = int(input("Insert an integer number >= 0 :"))
accum = 1
count = 1
if n >= 0:
while n >= count:
accum = accum * a
count += 1
# We already know n >= 0, and the only way
# for the loop to exit is for n < count to be
# true, so no additional testing needed before
# we call print here.
print(accum)
else: # If n >= 0 is not true, n < 0 *must* be true
print("Integer value is less than 0")
It's invalid because you firstly need to add an if statement and then follow it with an elif.
if n < 0:
print("Integer value is less than 0")
elif n >=0 and n < count:
print(accum)
You need to change the elif and if statement position
while n >= count and n >= 0:
accum = accum * a
count += 1
if n >=0 and n < count:
print(accum)
elif n < 0:
print("Integer value is less than 0")
Else if always comes after if
An Emirp is a prime number whose reversal is also a prime. For
example, 17 is a prime and 71 is a prime, so 17 and 71 are emirps.
Write a program that prints out the first N emirps, five on each line.
Calculate the first N emirp (prime, spelled backwards) numbers, where
N is a positive number that the user provides as input.
Implementation Details
You are required to make use of 2 functions (which you must write).
isPrime(value) # Returns true if value is a prime number. reverse
(value) # Returns the reverse of the value (i.e. if value is 35,
returns 53). You should use these functions in conjunction with logic
in your main part of the program, to perform the computation of N
emirps and print them out according to the screenshot below.
The general outline for your program would be as follows:
Step 1: Ask user for positive number (input validation) Step 2:
Initialize a variable Test to 2 Step 3: While # emirps found is less
than the input:
Call isPrime with Test, and call it again with reverse(Test).
If both are prime, print and increment number of emirps found. Test++ Hint - to reverse the number, turn it into a string and then
reverse the string. Then turn it back into an int!
MY CODE:
n = 0
count = 0
i = 1
def isPrime(value):
test = 2
count = 0
while(test < value):
if( value % test == 0):
count+=count
test+=test
if(count == 0):
return 1
else:
return 0
def reverse(value):
reverse = 0
while(value > 0):
reverse = reverse * 10 + (value % 10)
value = value / 10
return reverse
n = float(input("Please enter a positive number: "))
while(count < n):
i+=i;
if(isPrime(i)):
if(isPrime(reverse(i))):
print("i" + "\n")
count+=count
if((count % (5)) == 0 ):
print("\n")
where are the mistakes?
UPDATED CODE BUT STILL NOT RUNNING:
n = 0
count = 0
i = 1
def isPrime(value):
test = 2
while(test < value):
if( value % test != 0):
test +=1
else:
return 0
def reverse(value):
return int(str(value)[::-1])
i = int(input("Please enter a positive number: "))
count = 0
while(count < 5):
if(isPrime(i)):
if(isPrime(reverse(i))):
print(str(i) + "\n")
count += 1
i += 1
else:
i += 1
else:
i +=1
There are a lot of issues with your code. I altered the function isPrime. There is among other no need for count here and if you want to increment test by 1 every loop use test +=1:
def isPrime(value):
test = 2
while(test < value):
if( value % test != 0):
test +=1
else:
return 0
return 1
There is an easy way to reverse digits by making value a string in reversed order and to convert this into an integer:
def reverse(value):
return int(str(value)[::-1])
You among other have to assign the input to i. n in your code is a constant 5. You have to increment i by one in any condition and increment the count by one if i is an emirp only:
i = int(input("Please enter a positive number: "))
count = 0
while(count < 5):
if(isPrime(i)):
if(isPrime(reverse(i))):
print(str(i) + "\n")
count += 1
i += 1
else:
i += 1
else:
i +=1
def emrip_no(num):
i=0
j=0
for i in range(1,num+1):
a=0
for j in range(1,i+1):
if(i%j==0):
a+=1
if(a==2):
print("Number is a prime number")
k=0
l=0
rv=0
while(num!=0):
r=num%10
rv=(rv*10)+r
num=num//10
print("It's reverse is: ",rv)
for k in range(1,rv+1):
b=0
for l in range(1,k+1):
if(k%l==0):
b+=1
if(b==2):
print("It's reverse is also a prime number")
else:
print("It's reverse is not a prime number")
n=int(input("Enter a number: "))
emrip_no(n)
Say I have the following Python 3 code:
def foo(number_a, number_b):
if number_a >= 0:
print("a is Positive")
if number_b <= 100:
print("b is <= 100")
else:
print("a is negative and b > 100")
How can I have the else fire if and only if both conditions are false?
I could do something like this:
doElse = True
if condition1:
doElse = False
# do something
if condition2:
doElse = False
# do something
# condition3...conditionN
if doElse:
# do the else
But that would require setting the variable, then changing it for every if
Is there a better way to do this?
As long as your first and second condition are mutally exclusive, you should always solve this with a single if/elif/else construct. Using your original example:
if word == "banana":
print("word is banana")
elif word == "apple":
print("word is not banana, but it is apple")
else:
print("word is neither banana, nor apple, but something else")
If you have conditions which are not mutally exclusive, then this obviously doesn’t work as you want to continue checking the other conditions as well.
In that case, you should try to find a common condition for all of them. This can be as simple as combining the conditions with or:
if number >= 0 or number <= 100:
if number >= 0:
print("Positive")
if number <= 100:
print("<= 100")
else:
print("x < 0 or x > 100")
But you could also combine them if that’s possible:
if 0 <= number <= 100:
…
Or you could replace the else by an inverted condition:
if number >= 0:
print("Positive")
if number <= 100:
print("<= 100")
if number < 0 or number > 100:
print("x < 0 or x > 100")
For everything more complicated than that, you will have to keep track of this in another way, involving more code.
You can just reorder the if statements like so:
def foo(number_a, number_b):
if number_a < 0 and number_b > 100:
print("a is negative and b > 100")
elif number_a >= 0:
print("a is Positive")
else:
print("b is <= 100")