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生成器的使用方法和示例代码

本文是《Effect Python 编写高质量Python代码的59个有效方法》的学习笔记。主要记录生成器的使用方法和示例代码。 返回队列的函数 如果函数要产生一系列结果,那么最简单...

使用Python的SymPy库解决数学运算问题的方法

摘要:在学习与科研中,经常会遇到一些数学运算问题,使用计算机完成运算具有速度快和准确性高的优势。Python的Numpy包具有强大的科学运算功能,且具有其他许多主流科学计算语言不具备的免...

关于python pycharm中输出的内容不全的解决办法

关于python pycharm中输出的内容不全的解决办法

很多时候我们会发现有的时候输出的结果特别多的时候,会在最后输出时用。。。代替,最后输出一个总长度,那要咋么弄咧? import pandas as pd # 设置显示的最大列、宽等参...

python中global用法实例分析

本文实例讲述了python中global用法。分享给大家供大家参考。具体分析如下: 1、global---将变量定义为全局变量。可以通过定义为全局变量,实现在函数内部改变变量值。 2、一...

Python输出9*9乘法表的方法

本文实例讲述了Python输出9*9乘法表的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/env python # 9 * 9 for i in range(...