An Example of Calculating Digital Square in Python Programming

  • 2021-09-24 23:05:05
  • OfStack

Problem description:

Squared the input number. If the squared number is less than 100, exit.

Source code:


#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
TRUE = 1
FALSE = 0
def SQ(x):
  return x * x
print ' If the number you enter is less than  100 The program will stop running. '
again = 1
while again:
  num = int(raw_input(' Please enter 1 Numbers: '))
  print ' The result of the operation is : %d' % (SQ(num))
  if SQ(num) >= 100:
    again = TRUE
  else:
    again = FALSE

The output is as follows:


 If the number you enter is less than  100 The program will stop running. 
 Please enter 1 Numbers: 12
 The result of the operation is : 144
 Please enter 1 Numbers: 14
 The result of the operation is : 196
 Please enter 1 Numbers: 20
 The result of the operation is : 400
 Please enter 1 Numbers: 30
 The result of the operation is : 900
 Please enter 1 Numbers: 11
 The result of the operation is : 121
 Please enter 1 Numbers: 100
 The result of the operation is : 10000
 Please enter 1 Numbers: 21
 The result of the operation is : 441
 Please enter 1 Numbers: 8
 The result of the operation is : 64

Add: Find the square of the input number, if the square operation is less than 50, exit


while True:
num=int(input(' Please enter a number '))
s=num*num
if s<50:
break

Related articles: