笨方法学python 习题13

参数、解包和变量

学习目标:
了解将变量传递给脚本的方法(脚本就是.py程序)
例如*:要运行ex13.py,只要在命令输入python3.6 ex13.py,而ex13.py就是参数(argument),接下来就是要了解可以接收参数的脚本。*

代码如下:

from sys import argv
# read the WYSS section for how to run this
script, first, second, third = argv,'first','second','third'
#scripts,first, second, third是参数
#代码期望有4个参数,但现在只有1个(第一个参数始终为脚本名称)。 
#可以在pycharm中配置参数。:转到Run-> Edit Configurations, 然后创建一个新的python配置。
#可以指定Script parameters字段
#可以按照dnit13所述通过 arg1 arg2 arg3从命令行运行脚本
print('The script is called:',script)
print('Your first variable is:',first)
print('Your second variable is:',second)
print('Your third variable is:',third)

输出:

The script is called: ['D:/project/练习/习题13.py']
Your first variable is: first
Your second variable is: second
Your third variable is: third

巩固练习:

  1. 给你的脚本至少3个参数,看看会得到什么出错的消息,试着解释一下
from sys import argv
script, name, age, nationality = argv,'Marry','10',
print('Your name is called:',Marry)
print('Your age is:',age)
print('Your nationality is:',nationality)

#报错:
ValueError: not enough values to unpack (expected 4, got 3)
#期望值有4个,只给了3个,还缺一个

#修正:
from sys import argv
script, name, age, nationality = argv,'marry','10','China'
print('Your name is called:',name)
print('Your age is:',age)
print('Your nationality is:',nationality)

#结果:
Your name is called: marry
Your age is: 10
Your nationality is: China
  1. 再来两个,一个接收更少的参数,一个接收更多的参数,在参数解包时给他们取一些有意义的变量名
#1_2个变量
from sys import argv
script, name, heigh = argv,'Jonn','170',
print('Your name is:',name)
print('Your heigh is:',heigh)

#1结果
Your name is: Jonn
Your heigh is: 170

#2_5个变量
from sys import argv
script, white, Lucy, noodle,math,regression = argv,'white','Lucy','noodle','math','regression'
print('''What's your favorite color?:''',white)
print('''who's your favorite star?:''',Lucy)
print('''What's your favorite food?''',noodle)
print('''What is your favorite subject??''',math)
print('''What is your favorite statistical method?''',regression)

#2结果
What's your favorite color?: white
who's your favorite star?: Lucy
What's your favorite food? noodle
What is your favorite subject?? math
What is your favorite statistical method? regression
  1. 将input()和argv()一起使用
#复习一下
#input()有提示的作用,可以在()之间放入想要提示的字符串
#例如,y = input('name?')
#用name提示用户,将用户输入的结果赋值给变量y,

#argv()用参数给变量赋值

from sys import argv

script,  Jonn = argv,'Jonn'
y = input('put your name or ID in it')
print(f'Hi, I am the {Jonn}')


###输出:
put your name or ID in it
Hi, I am the Jonn

进程已结束,退出代码0
  1. 记住模块为你提供特性

Tips:

  1. argv即参数变量(argument variable),这个变量保存着运行python脚本时传递给python脚本的参数
  2. 将argv解包(unpack),与其将所有参数放在同一个变量下面,不如将参数赋值给4个变量:script, first, second, third。也就是,将argv的东西取出,解包,将所有的参数依次赋值给左边的变量:
#例如:
script, first, second, third = argv,'first','second','third'
  1. 代码导入(import)的特性称为模块(modules),例:将sys模块/库导入代码
  2. 如果运行程序遇到了:
#报错内容:
ValueError:not enough values to unpack.
#函数定义的返回值数量和调用函数时实际赋予的返回值数量不一致
#要么增加参数,要么减少参数
#要么检查opencv版本是否过低
  1. argv( )input( ) 的区别在于用户输入的时机:
    — 如果参数是在用户执行命令时就要输入需要用 argv( )
    — 如果在脚本执行过程中需要用户输入需要用 input( )

  2. 命令行参数是字符串吗?
    命令输入的是数字,需要用int()先转成整数,例如,int(input())

  3. 给左边赋值

input('?')=x #不灵?

#应该是
x = input('?')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值