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格式化输出保留2位小数的实现方法

python格式化输出保留2位小数的实现方法

我是小白就不用多说了,学习python做了个练习题,结果运行了一遍,发现输入金额后得到的有很多位小数, 虽然不知道为什么,但是看得很不舒服, 就想到应该把让小数点后只保留2位数 找到了方...

Django Rest framework认证组件详细用法

Django Rest framework认证组件详细用法

本文详细讲述了DRF认证组件的原理以及用法. 源码剖析 讲解DRF版本的时候我们都知道了,在dispatch方法里执行了initial方法来初始化我们的版本. 而在initial方法里有...

django ModelForm修改显示缩略图 imagefield类型的实例

在使用django的modelform的时候,修改表单,图片在form表单显示的是一个链接。显示缩略图如下 第一步: from django.forms.widgets import...

python使用itchat模块给心爱的人每天发天气预报

本文实例为大家分享了python给心爱的人每天发天气预报的具体代码,供大家参考,具体内容如下 下面的代码实现了用了之前获取天气的代码,然后用itchat模块 给指定的人发送消息 代码比...

Python多线程同步Lock、RLock、Semaphore、Event实例

Python多线程同步Lock、RLock、Semaphore、Event实例

一、多线程同步 由于CPython的python解释器在单线程模式下执行,所以导致python的多线程在很多的时候并不能很好地发挥多核cpu的资源。大部分情况都推荐使用多进程。 pyth...