Please help me to solve this issue! Python 3 code:
x=True
while x:
a=input('Enter an num:')
y=a.isdigit()
while y:
b=input('Enter an alphabet:')
z=b.isalpha
while z:
print('This is a nested while loop')
z=False
y=False
x=False
break
else:
print('wrong alphabet, Please try again !')
else:
print('wrong num format, Please try again !')
when I run this code and enter: Enter a num:123 Enter an alphabet: dog This is a nested while loop wrong num format, Please try again!
I don't want to print the wrong num format text. I also used exit to control the loop but not helpful. Please suggest me better ans.
In evaluating your code I did some simplification to the code along with changing out some of the "while" statements with "if" statements. Following is a revised code snippet.
while True:
a=input('Enter an number: ')
if a.isdigit(): # Instead of storing a boolean value, plus it works with the else statement below
while True:
b=input('Enter an alphabetic character: ')
if b.isalpha():
print('This is a nested while loop - goodbye')
quit() # Instead of breaking out of multiple while loops
else:
print('Wrong alphabet, Please try again !')
else:
print('Wrong number format, Please try again !')
Here are some points to note.
Testing that out, following is some sample terminal output.
@Dev:~/Python_Programs/Nested$ python3 Nested.py
Enter an number: e
Wrong number format, Please try again !
Enter an number: 3445
Enter an alphabetic character: w3e
Wrong alphabet, Please try again !
Enter an alphabetic character: Hello
This is a nested while loop - goodbye
Give that a try to see if that meets the spirit of your project.