Advertisement
DeaD_EyE

qsum_single

Nov 13th, 2020
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. def qsum_calc(value):
  2.     result = 0
  3.     while value:
  4.         result += value % 10
  5.         value //= 10
  6.     return result
  7.  
  8.  
  9. def qsum_single(value):
  10.     while value >= 10:
  11.         value = qsum_calc(value)
  12.     return value
  13.  
  14.  
  15. def get_value():
  16.     while True:
  17.         value = input("Please enter a value between 0 and 999: ")
  18.         try:
  19.             value = int(value)
  20.         except ValueError:
  21.             print(f"'{value}' is not a valid integer.")
  22.             continue
  23.         if 0 <= value <= 999:
  24.             return value
  25.         else:
  26.             print(f"'{value}' is not between 0 and 999")
  27.  
  28.  
  29. def main():
  30.     while True:
  31.         value = get_value()
  32.         result = qsum_single(value)
  33.         print(f"Result: {result}")
  34.  
  35.  
  36. if __name__ == "__main__":
  37.     try:
  38.         main()
  39.     except KeyboardInterrupt:
  40.         print()
  41.         print("Goodbye")
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement