美文网首页
《笨办法学Python》记录-part3(11~)

《笨办法学Python》记录-part3(11~)

作者: 仙芽子 | 来源:发表于2017-10-22 18:58 被阅读0次

11题

原来代码,用Py3运行报错,说raw_input()没有定义

修改后代码:

# 习题11:提问 IPO(Input Process Output)
print ("How old are you?")
age = input()#这里用了一个输入函数,py2里input变为raw_input()
print ("How tall are you?")
height= input()#这里用了一个输入函数,py2里input变为raw_input()
print ("How much do you weigh?")
weight = input()#这里用了一个输入函数,py2里input变为raw_input()

print ("So,you're %r old,%r tall and %r heavy."%(age,height,weight))# 这里要好好体会,这个规则是怎么运行的。

将原来代码改了后,运行结果如下:

Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable.
Visit http://www.python.org/download/mac/tcltk/ for current information.

=========== RESTART: /Users/yyy/Documents/Python_LPTHW/ex11py3.py ===========
How old are you?
15
How tall are you?
175cm
How much do you weigh?
60kg
So,you're '15' old,'175cm' tall and '60kg' heavy.
>>> 

上面的代码是我凭着自己的感觉在py2上改的py3,下面这个是刚找到的LP3THW(Learn Python 3 The Hard Way)里的实现方法:

完全的PY3 应该这样表达:

LP3THW里的源代码是这样的:

print ("How old are you?",end=' ')
age= input()
print("How tall are you?",end=' ')
height= input()
print("How much do you weight?",end=' ')
weight=input()
print (f"So, you`re {age} old ,{height} tall and {weight} heavy.")# 这里为啥用了f {}  这样的表达方式,回头再研究。
# 上述代码在py3里调试通过。

运行后的结果是:

Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable.
Visit http://www.python.org/download/mac/tcltk/ for current information.

========= RESTART: /Users/yyy/Documents/Python_LPTHW/ex11_new_py3.py =========
How old are you? 15
How tall are you? 175
How much do you weight? 85kg
So, you`re 15 old ,175 tall and 85kg heavy
>>> 

LOG

  • 20171122创建笨文件,完成了两个版本的ex11,发现其还是有一些区别的;另外找到pdf版本的LP3THW,和川聊了一下,准备将LP2THW和LP3THW分别刷一遍,他将3比2好多了,但是LPTHW的作者吐槽说,3会害死这门语言的,将来再补充把。;川推荐了一个网站Practice Python,看了一下,确实很好等我分别刷完了py2和py3之后在来练习一下这个网站吧。

相关文章

网友评论

      本文标题:《笨办法学Python》记录-part3(11~)

      本文链接:https://www.haomeiwen.com/subject/vrfnuxtx.html