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编写一个国际象棋AI程序

用Python编写一个国际象棋AI程序

最近我用Python做了一个国际象棋程序并把代码发布在Github上了。这个代码不到1000行,大概20%用来实现AI。在这篇文章中我会介绍这个AI如何工作,每一个部分做什么,它为什么能...

在pycharm 中添加运行参数的操作方法

在pycharm 中添加运行参数的操作方法

最近又重新看手上的代码,之前弄不明白的地方这次要一次弄明白。 代码中出现了很多sys.arfv[]的运行参数,pycharm怎么添加运行参数呢 打开Run->Edit Config...

python中Pycharm 输出中文或打印中文乱码现象的解决办法

python中Pycharm 输出中文或打印中文乱码现象的解决办法

1. 确保文件开头加上以下代码: # -*- coding:utf-8 -*- 还可以加上 import sys reload(sys) sys.setdefaulte...

浅谈flask截获所有访问及before/after_request修饰器

本文主要研究的是flask如何截获所有访问,以及before_request、after_request修饰器的相关内容,具体如下。 在学习着用flask开发安卓后天接口时,遇到一个需求...

Win10+GPU版Pytorch1.1安装的安装步骤

Win10+GPU版Pytorch1.1安装的安装步骤

安装cuda 更新nvidia驱动 打开GeForce Game Ready Driver或在GeForce Experience中下载符合自己gpu的程序。 选择cuda 打开nvi...