笨方法学python11-15

习题11:提问

代码

这部分根据搜索eval以及input内容,自己加多了一点小练习。熟悉一下eval

print("How old are you?",end="")
age = input()
print("How tal are you?",end="")
height = input()
print("How much do you weigh?",end="")
weight = input()
print(f"So,you're {age} old,{height} tall and {weight} heavy.")

#进行数学运算
a=eval(input("读取用户输入的数目来进行数学运算"))
print(a)

x=int(input("第一个数:"))
y=int(input("第二个数:"))
print(x,y,x+y)

h,j=eval(input("请输入两个数字:"))
print(h,j,h+j)
PS C:\Users\WU\pyfile> python ex11.py
How old are you?20
How tal are you?20
How much do you weigh?20
So,you're 20 old,20 tall and 20 heavy.
读取用户输入的数目来进行数学运算1
1
第一个数:1
第二个数:2
1 2 3
请输入两个数字:1,2
1 2 3

知识点

  • 一般软件做的事情:IPO(Input,Process.Output)
1.	接收输入的内容  input
2.	改变输入的内容  process
3.	打印出改变了的内容 print
  • print语句中end=‘ ’的用法,意思是不让用换行符跑到下一行,也就是直接在这一行输入内容。我们可以看一下print的help
PS C:\Users\WU\pyfile> python
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> help(input)
Help on built-in function input in module builtins:
input(prompt=None, /)
    Read a string from standard input.  The trailing newline is stripped.
    The prompt string, if given, is printed to standard output without a
    trailing newline before reading input.
    If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
    On *nix systems, readline is used if available.
>>> quit()
PS C:\Users\WU\pyfile>

可以查看一下我当时搜索感觉不错的帖子
python3中input输入浅谈

  • Python2和python3 的input是不一样的。
    Python3中,无论用户的输入内容是字符或是数字,input()函数会统一作为字符串类型输出。python2会将用户的输入内容可以作为表达式,然后计算表达式的结果赋值给前面的变量。在python2中可以通过raw_input可以实现python3中的input的功能,但Python3 是没有raw_input。

习题12:提示别人

代码

age = input("How old are you?")
height = input("How tall are you?")
weight = input("How much do you weigh?")
print(f"so ,you're {age} old,{height} tall and {weight} heavy.")

print("How old are you?",input())
PS C:\Users\WU\pyfile> python ex12.py
How old are you?20
How tall are you?20
How much do you weigh?20
so ,you're 20 old,20 tall and 20 heavy.
20
How old are you? 20

知识点

  • pydoc是python自带的一个文档生成工具,使用pydoc可以很方便查看类和方法结构.
  • 输入 q退出 pydoc ,当文档的内容很多的时候,窗口下出现more.
    输入q才会暂停,直接到末尾了。文档很短的就一屏显示完了。
  • print(“How old are you?”,input())
    程序运行起来没有任何提示,输入数字后直接才显示提示和输入的数字。这种方式不会产生错误,但是不符合使用逻辑。Input函数是人机交互的重要端口,应该是先显示input提示信息input,再存入变量里面。
  • 在windows 使用 python -m pydoc可以看各种命名了,open file os 和 sys的信息都很长,可以打开看一下。

习题13:参数、解包和变量

代码

from sys import argv
# read the WYSS section for how to run this
script,first,second,third = argv
#解包,将变量赋值给4个变量(argument variable)
print("The script is called:",script)
print("Your first variable is:",first)
print("Your second variable is:",second)
num=int(input("a num and add it before third:" ))
print("Your third variable is:",int(num+1-1))
PS C:\Users\WU\pyfile> python ex13.py 1 2 3
The script is called: ex13.py
Your first variable is: 1
Your second variable is: 2
a num and add it before third:5
Your third variable is: 5

知识点

  • 所谓脚本就是所编写的py文件
  • import让python程序更小,并且可以导入大量的模块(module)或者叫做库(library)
  • argv和input()不同在于用户输入的时机。参数是在用户执行命令时候就要输入,那就用argv,适合封装的一些小脚本的使用。在运行脚本的时候就输入需要的文件信息和参数。脚本运行过程中需要用户输入,那就用input(),这更适合人机交互的程序。
  • 命令行输入的参数是字符串,假如需要输入的是数字,得用int()将其转化为整数,如:int(input())。

破坏程序

  • 破坏程序一:没有添加足够的命令行参数。
PS C:\Users\WU\pyfile> python ex13.py
Traceback (most recent call last):
  File "C:\Users\WU\pyfile\ex13.py", line 3, in <module>
    script,first,second,third = argv
ValueError: not enough values to unpack (expected 4, got 1)
PS C:\Users\WU\pyfile> python ex13.py 1
Traceback (most recent call last):
  File "C:\Users\WU\pyfile\ex13.py", line 3, in <module>
    script,first,second,third = argv
ValueError: not enough values to unpack (expected 4, got 2)
PS C:\Users\WU\pyfile> python ex13.py 11 12 13 14
Traceback (most recent call last):
  File "C:\Users\WU\pyfile\ex13.py", line 3, in <module>
    script,first,second,third = argv
ValueError: too many values to unpack (expected 4)

只可以输入与参数变量一致的变量。过多和过少都是不合适的。


习题14:提示和传递

代码

from sys import argv
script,user_name,assitant=argv
prompt = ">>>>>" #双引单引都可以,可以随意修改提示符

print(f"Hi {user_name}, I'm the {script} script,my name is {assitant}.")
print(f"I'd like to ask you a few question.")
print(f"Do you like me {user_name}?")
likes = input(prompt)

print(f"Where are you live {user_name}?")
lives = input(prompt)

print(f"What kind of computer do you have?")
computer = input(prompt)

print(f"""
Alright, so you said {likes} about liking me.
You live in {lives}.Not sure where that is.
And you have a {computer} computer.Nice.
""")

PS C:\Users\WU\pyfile> python ex14.py W TTO
Hi W, I'm the ex14.py script,my name is TTO.
I'd like to ask you a few question.
Do you like me W?
>>>>>YEs
Where are you live W?
>>>>>?
What kind of computer do you have?
>>>>>666

Alright, so you said YEs about liking me.
You live in ?.Not sure where that is.
And you have a 666 computer.Nice.

知识点:

  • 用“>"来作为输入提示符,这是个好的想法,对输入地方看起来更加直观,使用别的软件的时候也常见这种方式。游戏经常喜欢这样弄一下。对于定义了prompt这个变量,可以集中修改input的提示符
  • Tendy 计算机貌似好厉害,搜了一下。
  • 《Zork》是一个交互式故事计算机游戏,也是电子游戏历史上最早的一款文字冒险游戏《Adventure》是一款由微软公司出品的解迷型冒险游戏,具有随机化的程序。

习题15:读取文件

代码

from sys import argv #从sys包导入入模块argv
script,filename = argv #解包,赋值两个参数,脚本名词和文件名称
txt = open(filename)  #打开文件,并把文件存到变量txt中

print(f"Here's your file {filename}") #打印
print(txt.read())  #打印阅读文件内容
txt.close() #关闭文件,释放内存

print("Type the filename again:") #打印
file_again = input ("> ") #赋值,>作为提示符,作用与上一节一样。
txt_again = open(file_again)
#打开重新输入的文件名的文件并存入变量,这是第二次打开同一个文件
print(txt_again.read()) #打印重新输入的文件的内容
txt_again.close() #关闭文件,释放内存
PS C:\Users\WU\pyfile> python ex15.py ex15_sample.txt
Here's your file ex15_sample.txt:
This id Stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

Type the filename again:
>ex15_sample.txt
This id Stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

知识点

  • txt and txt_again 变量执行一下 close() ,处理完文件后关闭colse文件,对运行结果没有什么变化,会释放这部分程序所占有的内存……
  • 命令行输入 python -m pydoc open 查看open()函数的说明,复习习题12的内容,
    可以使用q退出pydoc.
  • 函数open之后返回一个文件对象。
  • python不会限制让你只打开一次文件,有时候多次打开同一文件也是必需的。
  • from sys import argv 的意思是从软件包sys中取出argv这个模块(特性)

破坏程序

  • 破坏程序一:read没有给()
txt_again = open(file_again)
#打开重新输入的文件名的文件并存入变量,这是第二次打开同一个文件
print(txt_again.read) #打印重新输入的文件的内容
txt_again.close() #关闭文件,释放内存
PS C:\Users\WU\pyfile> python ex15.py ex15_sample.txt
Here's your file ex15_sample.txt
This id Stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

Type the filename again:
> ex15_sample.txt
<built-in method read of _io.TextIOWrapper object at 0x000001B63370B2B0>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值