HOW TO: Python: Countdown until matching digits? - python

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')

Related

How to use continue in while loop?

I tried to use continue in a while loop to skip print number but it doesn't work.
num = int(input())
while int(num) > 0 :
num-=1
print(num)
if num == 6:
continue
elif num ==1:
print("8 Numbers Printed Successfully.")
break
#i want to remove number six
Try this
num = int(input())
while num > 0 :
num -= 1
if num == 6:
continue
elif num == 1:
print("8 Numbers Printed Successfully.")
break
print(num)
Your print line should be after the if-else block
num = int(input())
while int(num) > 0:
num -= 1
# print(num) => if you print here, it does not check the condition
if num == 6:
continue
elif num == 1:
print("8 Numbers Printed Successfully.")
break
# print number here
print(num)
First of all, how do you know it's been 8 numbers? The input can be any number. Secondly, if you want to print every number but 6 you need to move the num -= 1 too. Being there, it won't print the first number.
Try this if you don't insist on using continue:
num = int(input())
printed_numbers = 0
while int(num) > 0 :
if num != 6:
print(num)
printed_numbers += 1
num -= 1
print("{} Numbers Printed Successfully.".format(printed_numbers))
Or this, if you want to test continue:
num = int(input())
printed_numbers = 0
while int(num) > 0 :
if num == 6:
num -= 1
continue
print(num)
printed_numbers += 1
num -= 1
print("{} Numbers Printed Successfully.".format(printed_numbers))
Finally, If you're ok with the num -= 1 there:
num = int(input())
printed_numbers = 0
while int(num) > 0 :
num -= 1
if num == 6:
continue
print(num)
printed_numbers += 1
print("{} Numbers Printed Successfully.".format(printed_numbers))
NOTE: I'm using printed_numbers because if the input is less than 6 you will print all the numbers else you'll have one less. You can use this condition instead.
You are print the number before the condition. chnage the code as follows.
num = int(input())
while int(num) > 0 :
num-=1
if num != 6:
print(num)
elif num == 1:
print("8 Numbers Printed Successfully.")
break
#i want to remove number six

How do I make it so Python checks each possible output and if it matches it, prints it?

My current code is this:
y = int(input('Please enter a number from 1 - 100: '))
if y == 1:
print('Y is 1.')
elif y >= 5:
print('Y is high.')
elif y <= 5:
print('Y is low.')
elif y != 7:
print('Y is unlucky.')
elif y == 2 and y == 3:
print('Y is 2 or 3.')
elif y >= 4 and y <= 7:
print('Y is mid range.')
If the user inputs Y as being 6, how do I make it so that it prints all of the true statements (shown below):
Y is high
Y is unlucky
Y is mid range
Solution:
y = int(input('Please enter a number from 1 - 100: '))
if y == 1:
print('Y is 1.')
if y >= 5:
print('Y is high.')
elif y <= 5:
print('Y is low.')
if y != 7:
print('Y is unlucky.')
if y == 2 and y == 3:
print('Y is 2 or 3.')
if y >= 4 and y <= 7:
print('Y is mid range.')
Explained:
if vs elif?
elif(abbreviation of term else if) condition evaluates only above if statement is not triggered. it has some distinction.
something like the code below.
The idea is to keep the functions in a list and invoke them by iterating the list
y = int(input('Please enter a number from 1 - 100: '))
def a(num):
if num == 1:
print('Y is 1.')
def b(num):
if num >= 5:
print('Y is high.')
functions = [a, b] # TODO: implement more functions
for func in functions:
func(y)
YOU CAN TRY COMBINING !=7 CONDITIONS USING LOGICAL OPERATORS TO EVERY OTHER CONDITIONS.
P.S. I THINK YOUR LINE 11 LOGIC IS WRONG TOO, YOU CAN EITHER INPUT '2 OR 3' AND NOT '2 AND 3'

Could someone explain why my code is not working properly?

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")

Python - How to use a float in a "if x in range" statement

I'm trying to write an if statement that takes a float as a range.
Thank you.
x = 8.2
if x in range(0, 4.4):
print('one')
if x in range(4.5, 8):
print('two')
if x in range(8.1, 9.9):
print('three')
if x > 10:
print('four')
I have also tried this to no avail
if x > 0 <= 4.4
Use
if 0 < x <= 4.4:
where x is in the middle. It's equivalent to
if 0 < x and x <= 4.4:
range is not suitable for that task.
You don't need range(). Just use comparisons, and use elif so that the ranges are exclusive.
if x < 4.5:
print('one')
elif x < 8:
print('two')
elif x < 10:
print('three')
else:
print('four')
This also solves the problem that you had gaps between your ranges.
x = 8.3
if 0 <= x <= 4.4:
print('one')
if 4.5 <= x <= 8:
print('two')
if 8.1 <= x <= 9.9:
print('three')
if x > 10:
print('four')

"out of range" else clause causes syntax error

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)

Categories

Resources