笨办法学python3练习代码13-14:argv参数变量的学习

ex13.py  argv参数的学习

 1 #argv:参数变量(argument variable),这是一个标准的编程术语,在其他语言中也可可以看到.argument可译为: 参数
 2 
 3 #如果参数是用户在执行命令时就要输入,用argv.命令行参数都是字符串
 4 #如果参数是在脚本运行过程中需要用户输入 ,用input()
 5 from sys import argv
 6 #read the WYSS section  for how to run this  
 7 
 8 #将argv解包(unpack),把参数赋值给4个变量:script,first,second,third 。例如执行时 python ex13.py 1  2  3 分别对应参数script,first,second,third.
 9 script,first,second,third = argv
10 #python ex13.py 1 22 33 与script,first,second,third一一对应
11 
12 print("The script is called: ",script)
13 print("Your first variable is: ",first)
14 print("YOur second variable is: ",second)
15 print("Your third variable is: ",third)
16 
17 
18 #与input()结合使用
19 x = input("what's your name ?.")
20 print(x)

ex14.py

 1 from sys import argv
 2 script, user_name = argv
 3 
 4 #把提示符赋值给prompt变量
 5 prompt = '>>>>'         #将字符串'>>>>' 赋给变量prompt
 6 
 7 #print(f"字符串"),前面的f表示print为格式化输出
 8 print(f"Hi {user_name}, I'm the {script} script.")
 9 print("I'd like to ask you a few question.")
10 print(f"Do you like me {user_name}?")
11 likes = input(prompt) #原样输出prompt = '>>>>'这个提示符后,读取用户自己输入的信息
12 
13 print(f"Where do you live {user_name}?")
14 lives = input(prompt) #原样输出prompt = '>>>>'这个提示符后,读取用户自己输入的信息
15 
16 print("What kind of computer do you have ?")
17 computer = input(prompt)
18 
19 #多行字符串格式化打印
20 print(f"""
21 Alright,so you said:{likes} about liking me.
22 You live in {lives}. Not sure where that is.
23 And you have a  {computer} computer .Nice
24 """)

 

posted @ 2020-07-17 21:50  lscv26  阅读(286)  评论(0)    收藏  举报