Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- num = ""#starts as empty string
- neg = False
- while type(num) == str:#loops so long as input remains string (cannot parse to int / float)
- num = input("Enter a number: ").strip()#new input
- if num[0] == "-":#check for negative number
- neg = True
- num = num[1:]#remove negative sign
- if num.isnumeric():#if can be parsed into int
- num = int(num)
- elif num.count(".") == 1 and num.replace(".","").isnumeric():#if has only one '.' and once removed can be parsed into int
- num = float(num)
- else:
- print("Invalid input, try again")
- neg = False#if invalid input started with '-', set negative back to false for the next number
- if neg:
- num = num*-1# if number was negative, add back negative sign by multiplying (-1)
- print("{}({})".format(str(type(num))[8:-2],num))#prints result as number, cast into class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement