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程序设计有所帮助。

相关文章

详谈python3中用for循环删除列表中元素的坑

for循环语句的对象是可迭代对象,可迭代对象需要实现__iter__或iter方法,并返回一个迭代器,什么是迭代器呢?迭代器只需要实现 __next__或next方法。 现在来验证一下列...

Python+tkinter使用80行代码实现一个计算器实例

Python+tkinter使用80行代码实现一个计算器实例

本文主要探索的是使用Python+tkinter编程实现一个简单的计算器代码示例,具体如下。 闲话不说,直奔主题。建议大家跟着敲一遍代码,体会一下代码复用、字符串方法的运用和动态创建组件...

Python中的pass语句使用方法讲解

 Python pass语句使用当语句要求不希望任何命令或代码来执行。 pass语句是一个空(null)操作;在执行时没有任何反应。pass也是代码最终会是有用的,但暂时不用写...

python解析文件示例

python最近的工作主要是组件兼容性测试,原有的框架有很多功能还不完善,需要补充!比如,需要将AutoIt脚本的执行结果写入到Excel中,最后的解决方案是使用本地的log来解析这个结...

python分割列表(list)的方法示例

前言 在日常开发中,有些API接口会限制请求的元素个数,这时就需要把一个大列表分割为固定的小列表,再进行相关处理,本文搜集了几个简单的方法,分享出来供大家参考学习,下面来看看详细的介绍:...