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中针对函数处理的特殊方法

很多语言都提供了对参数或变量进行处理的机制,作为灵活的Python,提供了一些针对函数处理的特殊方法filter(function, sequence): 对sequence中的item...

Python3 itchat实现微信定时发送群消息的实例代码

一、简介 1,使用微信,定时往指定的微信群里发送指定信息。 2,需要发送的内容使用excel进行维护,指定要发送的微信群名、时间、内容。 二、py库 1,itchat:这个是主要的工具,...

python实现批量转换文件编码(批转换编码示例)

复制代码 代码如下:# -*- coding:utf-8 -*-__author__ = 'walkskyer' import osimport glob class Encoding:...

python设定并获取socket超时时间的方法

python写法 import socket def test_socket_timeout(): s = socket.socket(socket.AF_INET,...

python实现协同过滤推荐算法完整代码示例

python实现协同过滤推荐算法完整代码示例

测试数据 http://grouplens.org/datasets/movielens/ 协同过滤推荐算法主要分为: 1、基于用户。根据相邻用户,预测当前用户没有偏好的未涉及物品...