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

yipeiwu_com6年前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更加充分的使用Sqlite3

我最近在涉及大量数据处理的项目中频繁使用 sqlite3。我最初的尝试根本不涉及任何数据库,所有的数据都将保存在内存中,包括字典查找、迭代和条件等查询。这很好,但可以放入内存的只有那么多...

Python中使用Counter进行字典创建以及key数量统计的方法

这里的Counter是指collections中的Counter,通过Counter可以实现字典的创建以及字典key出现频次的统计。然而,使用的时候还是有一点需要注意的小事项。 使用Co...

解决python删除文件的权限错误问题

使用os.remove删除文件,总是遇到错误:PermissionError: WinError 找了很久没找到什么原因,以为是windows系统的问题,最后发现是删除了一个没有关闭的文...

Python调用钉钉自定义机器人的实现

Python调用钉钉自定义机器人的实现

前言:由于公司使用钉钉,之前告警都是使用邮箱,但是这种协同效率比较低,所以调用钉钉机器人来实现实时告警。 创建机器人:创建钉钉群,然后添加群机器人。 python代码如下: #1、...

Django ModelForm组件使用方法详解

Django ModelForm组件使用方法详解

一、创建ModelForm from django.forms import ModelForm from appxx import models from django.form...