Skip to content

Instantly share code, notes, and snippets.

@KianYang-Lee
Created February 5, 2022 10:42
Show Gist options
  • Save KianYang-Lee/bc7c90518bcdb6afceeb120a57e5348c to your computer and use it in GitHub Desktop.
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
#!/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