Reputation: 5
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.
Upvotes: 0
Views: 11316
Reputation: 1
x = int(input())
while 11 <= x <= 100:
print(x, end='\n')
if x % 11 == 0:
break
x -= 1
else:
print("Input must be 11-100") #works for 4.25 DAT210
Upvotes: -2
Reputation: 19
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')
Upvotes: 0
Reputation: 1
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")
Upvotes: 0
Reputation: 6056
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")
Upvotes: 1