[10. Input and output] Zero-based learning python, simple and rude

Receive a single value entered by the user

  • It should be noted that any value entered by the user will be treated as a string;
  • If you want to participate in mathematical calculations, you must first convert its type to a numeric type;
        radius = input( " Please enter the calculated radius: " )  
        # Any input received by the input function is of type string
        print( " The radius you calculated is " , radius)
    
        # area = 3.14 * radius * radius #type error
        print( " The type of radius is: " , type(radius)) # String type (str), not numeric type
    
        # Only numeric types can perform math calculations
        radiusStr = input( " Please enter the calculated radius: " )
    
        # Convert the string to a value and reassign it to radius
        radius = eval(radiusStr)
        print( " The type of radius is: " , type(radius)) # integer type ( int )
    
        # Calculation results
        area = 3.14 * radius * radius
        print( " Your bread has: " , area, " so big " )    

     

    Receive multiple values ​​entered by the user at once

    • When the user enters, the values ​​need to be separated by commas;
    • The eval method must be used outside the input method, otherwise the data entered by the user will only be regarded as a normal string
      # Note that multiple values ​​are converted using the eval method
          width, height = eval(input( " Please enter the width and height of the rectangle: " ))
          print("width的类型为", type(width), "width=", width)
          print( " The type of height is " , type(height), " height= " , height)
          print( " The area of ​​the rectangle is: " , width * height, " square meter " )
      
          # Note that multiple values ​​are converted using the eval method
          a, b, c = eval(input( " Please enter three values: " ))
          iMax = max(a, b, c)
          iMin = min (a, b, c)
          print( "The maximum value is: " , iMax, "The minimum value is: " , iMin)

       

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325637098&siteId=291194637