Created
February 5, 2022 10:42
-
-
Save KianYang-Lee/bc7c90518bcdb6afceeb120a57e5348c to your computer and use it in GitHub Desktop.
Demonstrate the simplicity which walrus operator provides when writing a while loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Longer version without walrus operator | |
digit_list = [] | |
user_input = input('Type anything other than digits, and this prompt will quit: ') | |
while user_input.isdigit(): | |
digit_list.append(user_input) | |
user_input = input('Type anything other than digits, and this prompt will quit: ') | |
print('The digits entered by user are: ', digit_list) | |
# Shorter version using walrus operator | |
digit_list = [] | |
while (user_input := input('Type anything other than digits, and this prompt will quit: ')).isdigit(): | |
digit_list.append(user_input) | |
print('The digits entered by user are: ', digit_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment