Skip to content

Instantly share code, notes, and snippets.

@MarkMoretto
Last active July 22, 2018 10:02
Show Gist options
  • Save MarkMoretto/a925897b3c35a4d37c864b353ab14c55 to your computer and use it in GitHub Desktop.
Save MarkMoretto/a925897b3c35a4d37c864b353ab14c55 to your computer and use it in GitHub Desktop.
Simple demo of while loop and data type handling
import sys
from time import sleep
### Function that adds 1 to integer or float, not text
def Add_One(n):
return n + 1
### Function that determines input type and spits out designated result
def Calc_Number(num):
if num.find('.') != -1:
zz = float(data)
elif num.find('.') == -1:
try:
zz = int(data)
except TypeError:
print('Error! Number not detected.')
return Add_One(zz)
if __name__ == '__main__':
### This will loop unless exit conditions met
while True:
print('Please enter a number\nor press \'q\' to quit')
data = input('>>> ')
if data.lower() in ['q','quit','exit']:
break
else:
xyz = Calc_Number(data)
print('Your new number is: {0}'.format(xyz))
print('Goodbye!')
sleep(0.5)
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment