python获取从命令行输入数字的方法

yipeiwu_com5年前Python基础

本文实例讲述了python获取从命令行输入数字的方法。分享给大家供大家参考。具体如下:

#----------------------------------------
#      Name: numerical_input.py
#     Author: Kevin Harris
# Last Modified: 02/13/04
#  Description: This Python script demonstrates 
#         how to get numerical input
#         from the command line 
#         and use the if-else conditional.
#----------------------------------------
print()
print( "Welcome to the Area calculation program" )
print( "---------------------------------------" )
print()
# Print out the menu:
print( "Please select a shape:" )
print( "1 Rectangle" )
print( "2 Circle" )
print()
# The input function both prompts the user
# for input and fetches it...
shape = int( input( "> " ) )
# Calculate the area...
if shape == 1:
  height = int( input("Please enter the height: ") )
  width = int( input("Please enter the width: ") )
  area = height*width
  print( "The area is", area )
else:
  radius = int( input("Please enter the radius: ") )
  area = 3.14*(radius**2)
  print( "The area is", area )
input( '\n\nPress Enter to exit...' )

希望本文所述对大家的Python程序设计有所帮助。

相关文章

从零开始学Python第八周:详解网络编程基础(socket)

一,Socket编程 (1)Socket方法介绍 Socket是网络编程的一个抽象概念。通常我们用一个Socket表示“打开了一个网络链接“,而打开一个Socket需要知道目标计...

Python多层装饰器用法实例分析

本文实例讲述了Python多层装饰器用法。分享给大家供大家参考,具体如下: 前言 Python 的装饰器能够在不破坏函数原本结构的基础上,对函数的功能进行补充。当我们需要对一个函数补充不...

Python XlsxWriter模块Chart类用法实例分析

Python XlsxWriter模块Chart类用法实例分析

本文实例讲述了Python XlsxWriter模块Chart类用法。分享给大家供大家参考,具体如下: 一 点睛 Chart类是XlsxWriter模块中图表组件的基类,支持的图表类型包...

Python实现新浪博客备份的方法

本文实例讲述了Python实现新浪博客备份的方法。分享给大家供大家参考,具体如下: Python2.7.2版本实现,推荐在IDE中运行。 # -*- coding:UTF-8 -*-...

python getpass实现密文实例详解

python getpass实现密文实例详解

getpass模块的使用: 在python中实现密码密文需要导入getpass模块,在python中要使用内置模块的话,需要使用import进行导入,比如import getpass...