def Help(string):
while True:
if string == 'Manifest':
return Manifest()
break
elif string == 'Intent':
return Intent()
break
else:
print('The options available are: \n')
for i in andHelp:
print(i)
print('Type Q to Quit \n')
x = input('What option do you choose: ')
print('\n')
if x == 'Q':
break
else:
Help(x)
How come if it goes into the else statement it will keep looping?
for example:
"The options available are:
Intent
Manifest
Type Q to Quit
What option do you choose: " <-- this will keep coming up aswell as the function I choose.
You don't even need a while loop for what you are checking. Use this instead:
def Help(string):
if string == 'Manifest':
return Manifest()
elif string == 'Intent':
return Intent()
else:
print('The options available are:\n%s\nType Q to Quit\n' % '\n'.join(andHelp))
x = input('What option do you choose: ')
print('\n')
if x != 'Q':
Help(x)
note: I modified your prints a bit to reduce extra lines that really, didn't need to be there.
note2: As you may see in the comments, it may be dangerous to do this recursively without a limitation because you may reach a maximum depth level.
Your actual problem is due to your recursion not actually returning the inner frame's value, but eliminating the recursion seems a more straightforward solution.
What's goofy about this is doing a recursion inside a loop. Both the recursion and the loop serve the same purpose: To make the option choice keep happening until a valid option is given. So you can definitely eliminate one:
def Help(string):
while True:
if string == 'Manifest':
return Manifest()
break
elif string == 'Intent':
return Intent()
break
else:
print('The options available are: \n')
for i in andHelp:
print(i)
print('Type Q to Quit \n')
string = input('What option do you choose: ') # Change `x` to `string` so next iteration will change behavior
print('\n')
if string == 'Q':
break
As Inbar Rose's answer points out, you can shorten this quite a bit with recursion, but since Python requires a recursion limit, you can make the program crash by forcing it to recur beyond that limit. So perhaps sticking with the loop is better. Anyway, you can clean it up further by having the validation of string be the condition of the loop itself:
def Help(string):
validOptions = ('Manifest', 'Intent', 'Q')
while string not in validOptions:
print('The options available are: \n')
for i in andHelp:
print(i)
print('Type Q to Quit \n')
string = input('What option do you choose: ')
print('\n')
# Now you have a guaranteed-valid string, so you don't need this part in the loop.
if string == 'Manifest':
return Manifest() # No need for a 'break' after a return. It's [dead code](http://en.wikipedia.org/wiki/Dead_code)
elif string == 'Intent':
return Intent()
elif string == 'Q':
return
Related
The question I have is about the flag I have here for the while loop. This works but not like I think it should. I assume I'm not understanding something so if someone is able to explain, that would be great.
From my understanding this should break out of the loop as soon as one of my conditionals is met. So if I input 'q' it should break out and stop the loop. But what happens is it keeps going through the loop and then it breaks out. so it goes through the last prompt and prints the exception.
(Python version is 3.8.5)
# Statement that tells the user what we need.
print("Enter two numbers and I will tell you the sum of the numbers.")
# Lets the user know they can press 'q' to exit the program.
print("Press 'q' at anytime to exit.")
keep_going = True
# Loop to make the program keep going until its told to stop.
while keep_going:
# Prompt for user to input first number and store it in a variable.
first_number = input("First number: ")
# Create a break when entering the first number.
if first_number == 'q':
keep_going = False
# Prompt for user to input second number and store it in a variable.
second_number = input("Second number: ")
# Create a break when entering the second number.
if second_number == 'q':
keep_going = False
# Exception for non integers being input "ValueError"
try:
# Convert input to integers and add them.
# storing the answer in a variable.
answer = int(first_number) + int(second_number)
except ValueError:
# Tell the user what they did wrong.
print("Please enter a number!")
else:
# Print the sum of the numbers
print(f"\nThe answer is: {answer}")
Using this code it breaks out right away like I expect it to.
while True:
first_number = input("First number: ")
if first_number == 'q':
break
second_number = input("Second number: ")
if second_number == 'q':
break
I just would like to understand what the difference is and if thats how it should work. I feel like I'm missing something or misunderstanding something.
The condition of the while loop is only checked between iterations of the loop body, so if you change the condition in the middle of the loop, the current iteration will finish before the loop terminates. If you want to break a loop immediately, you need to either break (which automatically breaks the loop regardless of the condition) or continue (which jumps to the next iteration, and will therefore terminate the loop if the condition is no longer true).
Using while True: with a break when you want to stop the loop is generally much more straightforward than trying to control the loop by setting and unsetting a flag.
FWIW, rather than copying and pasting the code to input the two numbers, and have two different ways to break out of the loop, I might put that all into a function and break the loop with an Exception, like this:
print("Enter two numbers and I will tell you the sum of the numbers.")
print("Press 'q' at anytime to exit.")
def input_number(prompt: str) -> int:
"""Ask the user to input a number, re-prompting on invalid input.
Exception: raise EOFError if the user enters 'q'."""
while True:
try:
number = input(f"{prompt} number: ")
if number == 'q':
raise EOFError
return int(number)
except ValueError:
print("Please enter a number!")
while True:
try:
numbers = (input_number(n) for n in ("First", "Second"))
print(f"The answer is: {sum(numbers)}")
except EOFError:
break
This makes it easier to extend the program to handle more than two inputs; try adding a "Third" after where it says "First" and "Second"! :)
Once you run the program and type "q", Yes indeed keep_going will be set to False but it DOES NOT MEAN it will break the loop already, it will just make the keep_going be equal to False thus on the NEXT ITERATION will stop the loop. Why is that? because it would be like this while keep_going: -> while False: so since it is not True thus not executing the program anymore.
Now based on your goal as you mentioned. You can do it this way where you can add the break.
if first_number == 'q':
keep_going = False
break
# Prompt for user to input second number and store it in a variable.
second_number = input("Second number: ")
# Create a break when entering the second number.
if second_number == 'q':
keep_going = False
break
I'd also like to suggest have it this way, it's just more specific in terms of what is to happen on the code, but of course it is up to you.
first_number = input("First number: ")
# Create a break when entering the first number.
if first_number == 'q':
keep_going = False
break
# Prompt for user to input second number and store it in a variable.
# Create a break when entering the second number.
else:
second_number = input("Second number: ")
if second_number =='q':
keep_going = False
break
While loops execute until their given condition is false. A loop will only check its condition when required (program execution is moved to the top of the loop). In almost every case, this occurs when the full body of the loop has run. See here:
keep_going = True
while keep_going:
keep_going = False
# keep_going is False, but this will still print once
# because the loop has not checked its condition again.
print("Will execute once")
"Will execute once" prints a single time even after keep_going is set to False. This happens because the while loop does not re-check its condition until its entire body has run.
However, break statements are different. A break statement will cause a loop to exit immediately no matter what.
keep_going = True
while keep_going:
break # Exits the while loop immediately. The condition is not checked.
print("Will never print")
Here, nothing is printed even though keep_going is True the whole time. break made the loop exit regardless of the condition.
A continue statement will move program execution back to the start of the loop, and cause your condition to be checked again.
In this example, continue sends program execution back to the start of the loop. Since keep_going was set to False, nothing will print because the while loop will exit after realizing its condition evaluates to false.
keep_going = True
while keep_going:
keep_going = False
continue
print("Will never print")
First off, hope you have a great time learning Python!
Both ways will work and stop the loop, but there is a difference:
In the first method, you are changing the keep_going variable to false, therefore, the loop will stop when the the while loops finds out that keep_going had become False. However, the checking only happens at the end of the loop (In your case, it is after you have done your except or else part), the loop will not stop right away even when you entered q for your variable first_number.
In the second solution, you are using the break keyword in Python, to break away from the loop right away after you entered q for first_number.
Technically speaking, you will want to break if you want to break off from the loop right away when q is detected, otherwise, setting keep_going to False if you want the whole loop to be completed, but not run again for the next round.
In scenario 1 the result, even when you entered q,
Please enter a number!
Will always show, but not for scenario 2.
this is a little different approach to your script:
def main():
print("Enter two numbers and I will tell you the sum of the numbers.")
print("Press 'q' at anytime to exit.")
val = []
while True:
check_value = lambda x: 'quit' if x.lower() == 'q' or x.lower() == 'quit' else int(x)
if not val:
value = input("First number: ")
elif len(val) == 2:
answer = sum(val)
print(f"\nThe answer is: {answer}")
print('==='*15 + ' < ' + f'PROGRAM RESTARTING' + ' > ' + '==='*15)
val[:] = []
continue
else:
value = input("Second number: ")
try:
check_ = check_value(value)
val.append(check_)
except ValueError:
print("Please enter a number!")
continue
finally:
if check_ == 'quit':
print('Program is stopping....')
break
else:
pass
if __name__ == '__main__':
main()
It check at anytime the user's input, whether is a 'q' 'Q' or ('quit' or 'QUIT') or any combination or capital letter because run the check x.lower()
I suggest you to have a look at realpython.com especially the paragraph "he Python break and continue Statements."
Long story short:
Use Break to terminate the loop at any given time.
Use continue to roll back where you left and repeat the loop again (I used it in my code if the value is not a Int)
User pass to keep the loop running with no stop.
First time poster and I am very new to programming in general, (so please go easy on me) although I have worked in the IT industry as a help desk technician and field engineer.
I am in my first year at university and and have been asked to add a section on to a program to allow the user to remove entries that they have inputted into a list.
This is my code.
values = []
def programMenu():
print('This program will accept values until 0 is entered.')
choice = continueChoice()
if choice == 'Y':
ages = collectValues()
print('There are ', len(values),' in the data set.')
print('The values are as follows:', end ='')
print(values)
else:
return
def continueChoice():
print('Would you like to continue(Y/N):', end ='')
choice = str(input()).upper()
print(choice)
while choice != 'Y' and choice != 'N':
print('Invalid option. Would you like to continue(Y/N):', end ='')
choice = str(input()).upper()
return choice
def collectValues():
values = []
while True:
print ('Please enter each value:', end ="")
valValue = (int(input()))
if (valValue ==0):
break
values.append(valValue)
return values
def removeValues():
print(values)
print('Would you like to delete any entries?')
reage = str(input()).upper()
while reage == 'Y':
print('Which ages would you like to remove')
delVal = (int(input()))
values.remove(delVal)
else:
programMenu()
programMenu()
removeValues()
I have been trying to solve this for two days but I am getting an error "ValueError: list.remove(x): x not in list"
I have tried writing the removeValues code at the end of the operation to add values, I have tried to move the values = [] call around etc and have had no luck.
I checked the removeValues definition was actually taking the input by editing to get the program to print the value the user entered that they wished to be removed and that worked.
Any help is appreciated, I've only been coding a few months and I have emailed my lecturer but giving what's happening in the world at the moment the university are understaffed.
Thanks in advance.
before deleting the value from the list you need to add a if condition to check if value is present in the list or not for example:
if delVal in values:
values.remove(delVal)
else:
print('Value Not Found in the list')
and your while loop is in infinite loop as value of reage = 'Y' and you are not changing it inside the loop to stop.
you can use if statement instead of while.
def removeValues():
print(values)
print('Would you like to delete any entries?')
reage = str(input()).upper()
# if reage == 'Y'
if reage == 'Y':
print('Which ages would you like to remove')
delVal = (int(input()))
# check if delVal is present in the list or not
if delVal in values:
values.remove(delVal)
print('Deleted Age : '+ str(delVal))
print(values)
# if not then print the msg.
else:
print(str(delVal)+' Not Found in the list: ',str(values))
# you can uncomment the below code to again call removeValues function to ask for correct value to delete
#removeValues()
# if reage is not equal to 'Y' then it calls the programMenu again.
else:
programMenu()
You are not able to remove it because you are initializing the values in collectValues function each time. So the list is storing any value - just comment on this line and should work. Anyway, you also need to have exit criteria for each other functions.
values = []
def programMenu():
print('This program will accept values until 0 is entered.')
choice = continueChoice()
if choice == 'Y':
ages = collectValues()
print('There are ', len(values),' in the data set.')
print('The values are as follows:', end ='')
print(values)
else:
return
def continueChoice():
print('Would you like to continue(Y/N):', end ='')
choice = str(input()).upper()
print(choice)
while choice != 'Y' and choice != 'N':
print('Invalid option. Would you like to continue(Y/N):', end ='')
choice = str(input()).upper()
return choice
def collectValues():
#values = []
while True:
print ('Please enter each value:', end ="")
valValue = (int(input()))
if (valValue ==0):
break
values.append(valValue)
return values
def removeValues():
print(values)
print('Would you like to delete any entries?')
reage = str(input()).upper()
while reage == 'Y':
print('Which ages would you like to remove')
delVal = (int(input()))
values.remove(delVal)
else:
programMenu()
programMenu()
removeValues()
There are a few things that can be improved but I will try to keep the interface more or less the same.
The biggest issue is that functions in python have their own scope. That is, when you create a variable or list in a function, it is not shared in other functions.
What you need to do instead, (i think you had the right idea), is to create the list outside the functions and pass a reference to it as a parameter to each function:
values = []
def programMenu(values):
print('This program will accept values until 0 is entered.')
choice = continueChoice()
if choice == 'Y':
temp = collectValues()
values += temp # Append new values elementwise
print('There are ', len(values), ' in the data set.')
print('The values are as follows:', end='')
print(values)
else:
return
def removeValues(values):
print(values)
print('Would you like to delete any entries? (Y to continue)')
while str(input()).upper() == 'Y':
print('Which ages would you like to remove')
delVal = (int(input()))
values.remove(delVal)
print('Would you like to delete any entries? (pressY to continue)')
else:
programMenu(values)
programMenu(values)
removeValues(values)
The above functions need a reference to the initial list in order to manipulate it. Finally, a small change, in the removeValues() function, i moved the input in the while loop so that the question persists.
def collectValues():
values = [] # Not the same "values" list (not passed from reference)
while True:
print ('Please enter each value:', end="")
valValue = (int(input()))
if (valValue ==0):
break
values.append(valValue)
return values
If you have noticed, the collectValues() function does not need a reference since it only gets new values and later adds them to the values list. Also, by creating a new list in collectValues(), the other list is not affected ( it is entirely independent).
So I have a while true loop -
while True:
getStatus()
print('Ended')
I hope to be able to break out of it if answer is 999. this is what I tried:
def getStatus():
answer = input('What is the box ID? ')
if answer == 999:
return False
elif type(answer) == int:
boxId = answer + 1
print(boxId)
However even when the input is '999' it loops back and asks 'What is the box ID? ' again.
How do I get out of the while true loop?
Your while loop keeps looping because that's exactly what you've told it to do. After the function you call from its body returns, you ignore the return value, unconditionally print "Ended" and then do it all again, since the condition on the loop is obviously still truthy.
If you want the function to control if the loop keeps going, you should use its return value as the condition in the loop, with something like this:
running = True
while running:
running = getStatus()
print("Ended") # move this outside the loop!
This requires that getStatus returns a truthy value when you want to keep looping and a falsey value when you want to stop. Your current implementation of that function doesn't do that. It returns False when 999 is entered, but doesn't explicitly return anything if you give other input (which in Python is equivalent to returning None). Since both False and None are falsey, the code above won't actually work (you could patch it with something like running = getStatus() is None, but that would be horrible). You should change the function to have an explicit return statement in all of its branches (including the case for non-integer inputs, where it doesn't go into either your if or elif blocks).
If the loop and the function's logic are tightly intertwined, it might make sense to move the loop into the function itself, rather than having it be separate and needing to use a return value to signal when to stop. In a single function, you can use break to exit a loop directly:
def getStatus():
while True:
answer = input('What is the box ID? ')
if answer == 999:
break
elif isinstance(answer, int): # isinsance() is better than type(...) == ...
boxId = answer + 1
print(boxId)
else:
print("I'm sorry, I didn't understand that.") # still good to handle this case
you can add if statement before get_status() that will check if it's true and break and in the get_status function you will have to return true to break
def getStatus():
answer = input('What is the box ID? ')
if answer == 999:
return True
elif type(answer) == int:
boxId = answer + 1
print(boxId)
while True:
if getStatus():
print('Ended')
break
def selectedCountry():
while True:
country = input("Enter country of which you want to check pictures HR, RS, RO: ").upper()
if country == str("HR") or country == str("RO") or country == str("RS"):
break
else:
print("Please enter HR or RO or RS: " + "you wrote: " + country)
Why while True is working outside of a function and inside the same problem with asking again
Enter country of which you want to check pictures HR, RS, RO: >? hr
Enter country of which you want to check pictures HR, RS, RO:
You may change it to -
while True:
if(getStatus()==False):
print('Ended')
break
By this you can use returned value to break out of infinite loop.
I am new to programming and so i'm practicing a bid. At this point i'm practicing with functions. In the code below break and continue are outside the loop i can not see why. I've tryed out different ways but the only thing i get to work is the last block of code. Why are break and continue outside the loop here?
import random
again = 'y'
while again == "y" :
def main():
print "gues a number between 0 - 10."
nummer = random.randint(1,10)
found = False
while not found:
usergues = input("your gues?")
if usergues == nummer:
print 'Your the man'
found = True
else:
print 'to bad dude try again'
main()
again = raw_input('would you like to play again press y to play again press n yo exit')
if again == 'n':
break #here it says it is outside the loop
elif again != 'y':
print 'oeps i don\'t know what you mean plz enter y to play again or n to exit'
else:
continue #this is outside loop as well
#main()
Because you are new to programming, I will get a few basic tips in my answer too.
INFINITE LOOP
You are trying to start an infinite loop by first settingagain = 'y' and afterwards you are using this variable to evaluate a while loop. Because you are not changing the value of y, it is better to not use a variable to create this infinite loop. Instead, try this:
while True:
(some code)
DEFINE FUNCTION IN LOOP
You're defining the function main() inside of the while loop. As far as I can tell, there is no use for that. Just leave out the first while loop. If you define a function, it is permanent (much like a variable), so no need to redefine it everytime. Using your code, you won't even get to call the function, because you never end the first loop.
CONTINUE/BREAK NOT IN LOOP
The error is quite self-explanaitory, but here we go. If you would ever end the first loop (which in this case, you won't), the next thing you do is call your function main(). This will generate a number and make the user guess it until he got it right. When that happens, you get out of that function (and loop).
Next, you ask if the user would like to play again. This is just an input statement. You store the answer in the variable 'again'. You check, with an if statement (note that this is not a loop!) what the answer is. You want the user to play again if he typed 'y', so instead of using again != 'y', you could use the following:
if again == 'y':
main() # you call the function to play again
If 'n' was typed in, you want to exit the script, which you do not by typing break, because you are not in a loop, just in an if-statement. You can either type nothing, which will just go out of the if-statement. Because there is nothing after the if, you will exit the script. You could also useexit(), which will immediately exit the script.
Lastly, you want to repeat the question if neither of these two things were answered. You can put the if-statement inside of a loop. You can (if you want) use your break and continue when doing this, but you mostly want to avoid those two. Here is an example:
while True:
again = raw_imput('y for again or n to stop')
if again == 'y':
main()
exit() # use this if you don't want to ask to play again after the 2nd game
elif again == 'n':
print('bye!')
exit()
# no need for an 'else' this way
# every exit() can be replaced by a 'break' if you really want to
BASIC BREAK/CONTINUE USAGE
Finally, here is some basic usage of break and continue. People generally tend to avoid them, but it's nice to know what they do.
Using break will exit the most inner loop you are currently in, but you can only use it inside of a loop, obviously (for-loops or while-loops).
Using continue will immediately restart the most inner loop you are currently in, regardless of what code comes next. Also, only usable inside of a loop.
EVERYTHING TOGETHER
import random
again = 'y'
def main():
print ("gues a number between 0 - 10.")
nummer = random.randint(1,10)
found = False
while not found:
usergues = input("your gues?")
if usergues == nummer:
print ('Your the man')
found = True
else:
print ('to bad dude try again')
main()
while True:
again = input('would you like to play again press y to play again press n yo exit')
if again == 'n':
print ('bye!')
exit() # you could use break here too
elif again == 'y':
main()
exit() # you can remove this if you want to keep asking after every game
else:
print ('oeps i don\'t know what you mean plz enter y to play again or n to exit')
I hope I helped you!
You loops and def are all muddled, you want something more like:
import random
again = 'y'
while again == "y" :
print "gues a number between 0 - 10."
nummer = random.randint(1,10)
found = False
while not found:
usergues = input("your gues?")
if usergues == nummer:
print 'Your the man'
found = True
else:
print 'to bad dude try again'
while True:
again = raw_input('would you like to play again press y to play again press n to exit')
if again == 'n':
break
elif again != 'y':
print 'oeps i don\'t know what you mean plz enter y to play again or n to exit'
else:
break
You may want to refer to instructional material because you seem to misunderstand the general purpose of functions and the order of your logic.
Your function should be at the outer scope, e.g.:
def main():
again = 'y'
while again == "y" :
Your question for again needs to be indented into the while loop:
while again == "y":
[snip]
again = raw_input('would you like to play again press y to play again press n to exit')
if again == 'n':
break #here it says it is outside the loop
elif again != 'y':
print 'oops i don\'t know what you mean plz enter y to play again or n to exit'
else:
continue #this is outside loop as well
The else: continue is unnecessary because you are at the end of the loop.
However, this only asks the question once, and you probably want this in a while loop. You also don't need to check the again == "y" in the outer while loop, because you are controlling the flow here:
while True:
[snip]
again = raw_input("would you like to play again press y to play again press n to exit")
while again not in ('y', 'n'):
again = raw_input("oops i don't know what you mean plz enter y to play again or n to exit")
if again == 'n':
break
I would recommend against using a bare input() because any code could be executed, receiving a string and casting to an int would be safe (and you probably do some error checking):
usergues = int(raw_input("your guess?"))
Putting it all together it looks like:
def main():
while True:
print "guess a number between 1 - 10."
nummer = random.randint(1,10)
found = False
while not found:
usergues = int(raw_input("your guess?"))
if usergues == nummer:
print 'You're the man'
found = True
else:
print 'Too bad dude try again'
again = raw_input('would you like to play again press y to play again press n to exit')
while again not in ('y', 'n'):
again = raw_input('oops i don\'t know what you mean plz enter y to play again or n to exit')
if again == 'n':
break
main()
I have been assigned in my intro to computer science class to write a program that will print out a number that the user is asked to enter (20-99). I have been able to do that, and have created an error message if the user doesn't enter a number in this range. The issue I am having is when a number out of range is entered, the error message displays, but for some reason the program continues and still prints out a english number. I have been trying to figure out how to get the program to stop at the error message, but I have not been able to figure it out. Here is what I currently have.
a=int(input('Pick a number between 20 through 99:'))
b=a//10
c=a%10
while a<20 or a>99:
print('Error, enter number between 20 and 99:')
break
while a>20 or a<99:
if b==2:
print('The number is Twenty',end=' ')
elif b==3:
print('The number is Thirty',end=' ')
elif b==4:
print('The number is Fourty',end=' ')
elif b==5:
print('The number is Fifty',end=' ')
elif b==6:
print('The number is Sixty',end=' ')
elif b==7:
print('The number is Seventy',end=' ')
elif b==8:
print('The number is Eighty',end=' ')
else:
print('The number is Ninety',end=' ')
if c==1:
print('One')
elif c==2:
print('Two')
elif c==3:
print('Three')
elif c==4:
print('Four')
elif c==5:
print('Five')
elif c==6:
print('Six')
elif c==7:
print('Seven')
elif c==8:
print('Eight')
else:
print('Nine')
break
The second conditional you want and not or
while a>20 and a<99:
also use if because it will infinite loop if you don't
You have confused "while" with "if". You need an "if" statement here; "while" is for things you intend to repeat.
Also, note that a>20 or a<99 is always true; any number is one or the other. I believe you wanted an "and" here, which makes this merely the "else" clause of the "if" statement.
Finally, I'm not sure what you're trying to do with the "end=" in your first bank of print statements. This is a syntax error.
You have put the break statement inside of the while loop. This means that when you reach that statement, you leave the while loop. So no matter what, your function will leave the loop. A good sign that your while loop is not correct or out of place is if you call a break at the end. Here is a better way.
while True:
a=int(input('Pick a number between 20 through 99:'))
if a > 20 and a < 99:
break;
else:
print("Error, enter number between 20 and 99")
What happens is that the loop goes on indefinitely. Once the correct input is entered, it breaks from the loop. If the input is not correct, it just loops again.
Even though you didn't ask about it, I'm going to comment on the other half as well. First, your condition doesn't make sense. All numbers are either over 20 or under 99. You need to use an and so that BOTH must be true. However, the other part is that you don't even need this conditional statement. We already know that we are in this limit. That's what we made sure of in our previous while loop. Lastly, as stated before, the while itself isn't need. If you want to use a conditional that you only use one time, just use an if statement. While is mean for looping and forces you to have a break statement at the end if you will only ever use it once. Here is your completed code:
while True:
a=int(input('Pick a number between 20 through 99:'))
if a > 20 and a < 99:
break;
else:
print("Error, enter number between 20 and 99")
b=a//10
c=a%10
if b==2:
print('The number is Twenty',end=' ')
elif b==3:
print('The number is Thirty',end=' ')
elif b==4:
print('The number is Fourty',end=' ')
elif b==5:
print('The number is Fifty',end=' ')
elif b==6:
print('The number is Sixty',end=' ')
elif b==7:
print('The number is Seventy',end=' ')
elif b==8:
print('The number is Eighty',end=' ')
else:
print('The number is Ninety',end=' ')
if c==1:
print('One')
elif c==2:
print('Two')
elif c==3:
print('Three')
elif c==4:
print('Four')
elif c==5:
print('Five')
elif c==6:
print('Six')
elif c==7:
print('Seven')
elif c==8:
print('Eight')
else:
print('Nine') `enter code here`